13.2.14 VALUES Statement语句

VALUES is a DML statement introduced in MySQL 8.0.19 which returns a set of one or more rows as a table. VALUES是MySQL 8.0.19中引入的DML语句,它以表的形式返回一组一行或多行。In other words, it is a table value constructor which also functions as a standalone SQL statement.换句话说,它是一个表值构造函数,也可以作为一个独立的SQL语句。

VALUES row_constructor_list [ORDER BY column_designator] [LIMIT BY number]
row_constructor_list:
    ROW(value_list)[, ROW(value_list)][, ...]
value_list:
value[, value][, ...]
column_designator:
    column_index

The VALUES statement consists of the VALUES keyword followed by a list of one or more row constructors, separated by commas. VALUES语句包含VALUES关键字,后跟一个或多个行构造函数的列表,用逗号分隔。A row constructor consists of the ROW() row constructor clause with a value list of one or more scalar values enclosed in the parentheses. 行构造函数由ROW()行构造函数子句组成,该子句包含一个或多个标量值的值列表,这些标量值括在括号中。A value can be a literal of any MySQL data type or an expression that resolves to a scalar value.值可以是任何MySQL数据类型的文本,也可以是解析为标量值的表达式。

ROW() cannot be empty (but each of the supplied scalar values can be NULL). ROW()不能为空(但提供的每个标量值都可以为NULL)。Each ROW() in the same VALUES statement must have the same number of values in its value list.同一VALUES语句中的每个ROW()在其值列表中的值数目必须相同。

The DEFAULT keyword is not supported by VALUES and causes a syntax error, except when it is used to supply values in an INSERT statement.DEFAULT关键字不受VALUES支持,会导致语法错误,除非它用于在INSERT语句中提供值。

The output of VALUES is a table:VALUES的输出是一个表:

mysql> VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8);
+----------+----------+----------+
| column_0 | column_1 | column_2 |
+----------+----------+----------+
|        1 |       -2 |        3 |
|        5 |        7 |        9 |
|        4 |        6 |        8 |
+----------+----------+----------+
3 rows in set (0.00 sec)

The columns of the table output from VALUES have the implicitly named columns column_0, column_1, column_2, and so on, always beginning with 0. VALUES输出的表的列具有隐式命名的列column_0column_1column_2等等,始终以0开头。This fact can be used to order the rows by column using an optional ORDER BY clause in the same way that this clause works with a SELECT statement, as shown here:此事实可用于使用可选的ORDER BY子句按列对行进行排序,其方式与此子句处理SELECT语句的方式相同,如下所示:

mysql> VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8) ORDER BY column_1;
+----------+----------+----------+
| column_0 | column_1 | column_2 |
+----------+----------+----------+
|        1 |       -2 |        3 |
|        4 |        6 |        8 |
|        5 |        7 |        9 |
+----------+----------+----------+
3 rows in set (0.00 sec)

The VALUES statement also supports a LIMIT clause for limiting the number of rows in the output.VALUES语句还支持一个LIMIT子句来限制输出中的行数。

The VALUES statement is permissive regarding data types of column values; you can mix types within the same column, as shown here:VALUES语句允许列值的数据类型;可以在同一列中混合类型,如下所示:

mysql> VALUES ROW("q", 42, '2019-12-18'),
    ->     ROW(23, "abc", 98.6),
    ->     ROW(27.0002, "Mary Smith", '{"a": 10, "b": 25}');
+----------+------------+--------------------+
| column_0 | column_1   | column_2           |
+----------+------------+--------------------+
| q        | 42         | 2019-12-18         |
| 23       | abc        | 98.6               |
| 27.0002  | Mary Smith | {"a": 10, "b": 25} |
+----------+------------+--------------------+
3 rows in set (0.00 sec)
Important重要

VALUES with one or more instances of ROW() acts as a table value constructor; although it can be used to supply values in an INSERT or REPLACE statement, do not confuse it with the VALUES keyword that is also used for this purpose. 具有一个或多个ROW()实例的VALUES充当表值构造函数;尽管它可以用于在INSERTREPLACE语句中提供值,但不要将其与同样用于此目的的values关键字混淆。You should also not confuse it with the VALUES() function that refers to column values in INSERT ... ON DUPLICATE KEY UPDATE.也不应将其与引用INSERT ... ON DUPLICATE KEY UPDATE中列值的VALUES()函数混淆。

You should also bear in mind that ROW() is a row value constructor (see Section 13.2.11.5, “Row Subqueries”, whereas VALUES ROW() is a table value constructor; the two cannot be used interchangeably.您还应该记住,ROW()是一个行值构造函数(请参阅第13.2.11.5节,“行子查询”),而VALUES ROW()是一个表值构造函数;两者不能互换使用。

VALUES can be used in many cases where you could employ SELECT, including those listed here:VALUES可以用在很多你可以采用SELECT的情形中,包括下面列表的这些: