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语句。
VALUESrow_constructor_list
[ORDER BYcolumn_designator
] [LIMIT BYnumber
]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_0
、column_1
、column_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)
具有一个或多个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
充当表值构造函数;尽管它可以用于在INSERT
或REPLACE
语句中提供值,但不要将其与同样用于此目的的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
的情形中,包括下面列表的这些:
With 使用UNION
, as shown here:UNION
,如下所示:
mysql>SELECT 1,2 UNION SELECT 10,15;
+----+----+ | 1 | 2 | +----+----+ | 1 | 2 | | 10 | 15 | +----+----+ 2 rows in set (0.00 sec) mysql>VALUES ROW(1,2) UNION VALUES ROW(10,15);
+----------+----------+ | column_0 | column_1 | +----------+----------+ | 1 | 2 | | 10 | 15 | +----------+----------+ 2 rows in set (0.00 sec)
It is also possible in this fashion to union together constructed tables having more than one row, like this:也可以以这种方式将具有多行的构造表合并在一起,如下所示:
mysql>VALUES ROW(1,2), ROW(3,4), ROW(5,6)
>UNION VALUES ROW(10,15),ROW(20,25);
+----------+----------+ | column_0 | column_1 | +----------+----------+ | 1 | 2 | | 3 | 4 | | 5 | 6 | | 10 | 15 | | 20 | 25 | +----------+----------+ 5 rows in set (0.00 sec)
You can also (and it is usually preferable to) omit 在这种情况下,您还可以(通常更可取的做法是)完全省略UNION
altogether in such cases and use a single VALUES
statement, like this:UNION
,并使用单个VALUES
语句,如下所示:
mysql> VALUES ROW(1,2), ROW(3,4), ROW(5,6), ROW(10,15), ROW(20,25);
+----------+----------+
| column_0 | column_1 |
+----------+----------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 10 | 15 |
| 20 | 25 |
+----------+----------+
VALUES
can also be used in unions with SELECT
statements, TABLE
statements, or both.VALUES
也可以与SELECT
语句、TABLE
语句或两者结合使用。
The constructed tables in the UNION
must contain the same number of columns, just as if you were using SELECT
. UNION
中构造的表必须包含相同数量的列,就像使用SELECT
一样。See Section 13.2.10.3, “UNION Clause”, for further examples.更多示例请参阅第13.2.10.3节,“UNION子句”。
In joins. 用于联接。See Section 13.2.10.2, “JOIN Clause”, for more information and examples.有关更多信息和示例,请参阅第13.2.10.2节,“JOIN子句”。
In place of 在VALUES()
in an INSERT
or REPLACE
statement, in which case its semantics differ slightly from what is described here. INSERT
语句或REPLACE
语句中替换VALUES()
,在这种情况下,其语义与此处描述的略有不同。See Section 13.2.6, “INSERT Statement”, for details.详见第13.2.6节,“INSERT语句”。
In place of the source table in 在CREATE TABLE ... SELECT
and CREATE VIEW ... SELECT
. CREATE TABLE ... SELECT
和CREATE VIEW ... SELECT
中替换源表。See the descriptions of these statements for more information and examples.有关更多信息和示例,请参见这些语句的描述。