SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]select_expr
[,select_expr
] ... [into_option
] [FROMtable_references
[PARTITIONpartition_list
]] [WHEREwhere_condition
] [GROUP BY {col_name
|expr
|position
}, ... [WITH ROLLUP]] [HAVINGwhere_condition
] [WINDOWwindow_name
AS (window_spec
) [,window_name
AS (window_spec
)] ...] [ORDER BY {col_name
|expr
|position
} [ASC | DESC], ... [WITH ROLLUP]] [LIMIT {[offset
,]row_count
|row_count
OFFSEToffset
}] [into_option
] [FOR {UPDATE | SHARE} [OFtbl_name
[,tbl_name
] ...] [NOWAIT | SKIP LOCKED] | LOCK IN SHARE MODE] [into_option
]into_option
: { INTO OUTFILE 'file_name
' [CHARACTER SETcharset_name
]export_options
| INTO DUMPFILE 'file_name
' | INTOvar_name
[,var_name
] ... }
SELECT
is used to retrieve rows selected from one or more tables, and can include UNION
statements and subqueries. SELECT
用于检索从一个或多个表中选择的行,可以包括UNION
语句和子查询。See Section 13.2.10.3, “UNION Clause”, and Section 13.2.11, “Subqueries”. 参见第13.2.10.3节,“UNION子句”和第13.2.11节,“子查询”。A SELECT
statement can start with a WITH
clause to define common table expressions accessible within the SELECT
. SELECT
语句可以以WITH
子句开头,以定义SELECT
中可访问的公共表表达式。See Section 13.2.15, “WITH (Common Table Expressions)”.请参阅第13.2.15节,“WITH(公共表表达式)”。
The most commonly used clauses of SELECT
statements are these:SELECT
语句最常用的子句如下:
Each 每个select_expr
indicates a column that you want to retrieve. select_exp
r表示要检索的列。There must be at least one 必须至少有一个select_expr
.select_expr
。
table_references
indicates the table or tables from which to retrieve rows. table_references
表示要从中检索行的一个或多个表。Its syntax is described in Section 13.2.10.2, “JOIN Clause”.其语法在第13.2.10.2节,“JOIN子句”中描述。
SELECT
supports explicit partition selection using the PARTITION
clause with a list of partitions or subpartitions (or both) following the name of the table in a table_reference
(see Section 13.2.10.2, “JOIN Clause”). SELECT
支持使用PARTITION
子句进行显式分区选择,PARTITION
子句中的分区或子分区(或两者)列表位于table_reference
中的表名称之后(请参阅第13.2.10.2节,“JOIN子句”)。In this case, rows are selected only from the partitions listed, and any other partitions of the table are ignored. 在这种情况下,只从列出的分区中选择行,而忽略表中的任何其他分区。For more information and examples, see Section 24.5, “Partition Selection”.有关更多信息和示例,请参阅第24.5节,“分区选择”。
The WHERE
clause, if given, indicates the condition or conditions that rows must satisfy to be selected. WHERE
子句(如果给定)表示要选择行必须满足的一个或多个条件。where_condition
is an expression that evaluates to true for each row to be selected. where_condition
是一个表达式,对于要选择的每一行,它的计算结果都为true
。The statement selects all rows if there is no 如果没有WHERE
clause.WHERE
子句,则语句将选择所有行。
In the 在WHERE
expression, you can use any of the functions and operators that MySQL supports, except for aggregate (group) functions. WHERE
表达式中,除了聚合(分组)函数之外,您可以使用MySQL支持的任何函数和运算符。See Section 9.5, “Expressions”, and Chapter 12, Functions and Operators.参见第9.5节,“表达式”和第12章,“函数和运算符”。
SELECT
can also be used to retrieve rows computed without reference to any table.SELECT
还可用于检索不引用任何表而计算的行。
For example:例如:
mysql> SELECT 1 + 1;
-> 2
You are permitted to specify 在没有引用任何表的情况下,允许将DUAL
as a dummy table name in situations where no tables are referenced:DUAL
指定为伪表名:
mysql> SELECT 1 + 1 FROM DUAL;
-> 2
DUAL
is purely for the convenience of people who require that all SELECT
statements should have FROM
and possibly other clauses. DUAL
纯粹是为了方便那些要求所有SELECT
语句都应该有FROM
和其他子句的人。MySQL may ignore the clauses. MySQL可能会忽略这些子句。MySQL does not require 如果没有表被引用,MySQL不需要FROM DUAL
if no tables are referenced.FROM DUAL
。
In general, clauses used must be given in exactly the order shown in the syntax description. 一般来说,所使用的子句必须严格按照语法描述中所示的顺序给出。For example, a 例如,HAVING
clause must come after any GROUP BY
clause and before any ORDER BY
clause. HAVING
子句必须位于任何GROUP BY
子句之后、任何ORDER BY
子句之前。The INTO
clause, if present, can appear in any position indicated by the syntax description, but within a given statement can appear only once, not in multiple positions. INTO
子句(如果存在)可以出现在语法描述指示的任何位置,但在给定语句中只能出现一次,不能出现在多个位置。For more information about 有关INTO
, see Section 13.2.10.1, “SELECT ... INTO Statement”.INTO
的更多信息,请参阅第13.2.10.1节,“SELECT ... INTO语句”。
The list of select_expr
terms comprises the select list that indicates which columns to retrieve. select_expr
子句的列表包含指示要检索哪些列的select列表。Terms specify a column or expression or can use 术语指定列或表达式,也可以使用*
-shorthand:*
-速记:
A select list consisting only of a single unqualified 仅由单个非限定*
can be used as shorthand to select all columns from all tables:*
组成的选择列表可用作从所有表中选择所有列的缩写:
SELECT * FROM t1 INNER JOIN t2 ...
tbl_name
.* can be used as a qualified shorthand to select all columns from the named table:可以用作从命名表中选择所有列的限定缩写:
SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ...
If a table has invisible columns, 如果一个表有不可见的列,*
and
do not include them. tbl_name
.**
和tbl_name.*
不包括它们。To be included, invisible columns must be referenced explicitly.要包含不可见列,必须显式引用不可见列。
Use of an unqualified 将非限定*
with other items in the select list may produce a parse error. *
与选择列表中的其他项一起使用可能会产生分析错误。To avoid this problem, use a qualified 为避免此问题,请使用限定的
reference:tbl_name
.*tbl_name.*
引用:
SELECT AVG(score), t1.* FROM t1 ...
The following list provides additional information about other 以下列表提供了有关其他SELECT
clauses:SELECT
子句的其他信息:
A select_expr
can be given an alias using AS
. alias_name
select_expr
可以使用AS alias_name
来给定别名。The alias is used as the expression's column name and can be used in 别名用作表达式的列名,可以在GROUP BY
, ORDER BY
, or HAVING
clauses. GROUP BY
子句、ORDER BY
子句或HAVING
子句中使用。For example:例如:
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;
The 当用标识符为AS
keyword is optional when aliasing a select_expr
with an identifier. select_expr
取别名时,AS
关键字是可选的。The preceding example could have been written like this:前面的例子可以这样写:
SELECT CONCAT(last_name,', ',first_name) full_name FROM mytable ORDER BY full_name;
However, because the 但是,由于AS
is optional, a subtle problem can occur if you forget the comma between two select_expr
expressions: MySQL interprets the second as an alias name. AS
是可选的,如果您忘记了两个select_expr
之间的逗号,就会出现一个微妙的问题:MySQL将第二个表达式解释为别名。For example, in the following statement, 例如,在以下语句中,columnb
is treated as an alias name:columnb
被视为别名:
SELECT columna columnb FROM mytable;
For this reason, it is good practice to be in the habit of using 因此,在指定列别名时,最好养成显式使用AS
explicitly when specifying column aliases.AS
的习惯。
It is not permissible to refer to a column alias in a 不允许在WHERE
clause, because the column value might not yet be determined when the WHERE
clause is executed. WHERE
子句中引用列别名,因为在执行WHERE
子句时可能尚未确定列值。See Section B.3.4.4, “Problems with Column Aliases”.参见第B.3.4.4节,“列别名问题”。
The FROM
clause indicates the table or tables from which to retrieve rows. table_references
FROM table_references
子句指示要从中检索行的一个或多个表。If you name more than one table, you are performing a join. 如果命名了多个表,则执行的是联接。For information on join syntax, see Section 13.2.10.2, “JOIN Clause”. 有关连接语法的信息,请参阅第13.2.10.2节,“连接子句”。For each table specified, you can optionally specify an alias.对于指定的每个表,可以选择指定别名。
tbl_name
[[AS]alias
] [index_hint
]
The use of index hints provides the optimizer with information about how to choose indexes during query processing. 索引提示的使用为优化器提供了有关如何在查询处理期间选择索引的信息。For a description of the syntax for specifying these hints, see Section 8.9.4, “Index Hints”.有关指定这些提示的语法说明,请参阅第8.9.4节,“索引提示”。
You can use 您可以使用SET max_seeks_for_key=
as an alternative way to force MySQL to prefer key scans instead of table scans. value
SET max_seeks_for_key=value
作为另一种方法,强制MySQL选择键扫描而不是表扫描。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”。
You can refer to a table within the default database as 可以将默认数据库中的表引用为tbl_name
, or as db_name
.tbl_name
或db_name
。tbl_name
to specify a database explicitly. tbl_name
以显式指定数据库。You can refer to a column as 可以将列引用为col_name
, tbl_name
.col_name
, or db_name
.tbl_name
.col_name
. col_name
、tbl_name.col_name
或db_name.tbl_name.col_name
。You need not specify a 不需要为列引用指定tbl_name
or db_name
.tbl_name
prefix for a column reference unless the reference would be ambiguous. tbl_name
或db_name.tbl_name
前缀,除非引用不明确。See Section 9.2.2, “Identifier Qualifiers”, for examples of ambiguity that require the more explicit column reference forms.请参阅第9.2.2节“标识符限定符”,以了解需要更明确的列引用形式的歧义示例。
A table reference can be aliased using 表引用可以加别名,方法是使用
or tbl_name
AS alias_name
tbl_name alias_name
.
或tbl_name
AS alias_name
tbl_name alias_name
。These statements are equivalent:这些语句是等同的:
SELECT t1.name, t2.salary FROM employee AS t1, info AS t2 WHERE t1.name = t2.name; SELECT t1.name, t2.salary FROM employee t1, info t2 WHERE t1.name = t2.name;
Columns selected for output can be referred to in 可以使用列名、列别名或列位置按ORDER BY
and GROUP BY
clauses using column names, column aliases, or column positions. ORDER BY
和GROUP BY
子句引用为输出选择的列。Column positions are integers and begin with 1:列位置是整数,从1开始:
SELECT college, region, seed FROM tournament ORDER BY region, seed; SELECT college, region AS r, seed AS s FROM tournament ORDER BY r, s; SELECT college, region, seed FROM tournament ORDER BY 2, 3;
To sort in reverse order, add the 若要按相反顺序排序,请将DESC
(descending) keyword to the name of the column in the ORDER BY
clause that you are sorting by. DESC
(降序)关键字添加到排序依据的ORDER BY
子句中列的名称中。The default is ascending order; this can be specified explicitly using the 默认为升序;这可以使用ASC
keyword.ASC
关键字显式指定。
If 如果ORDER BY
occurs within a parenthesized query expression and also is applied in the outer query, the results are undefined and may change in a future version of MySQL.ORDER BY
出现在带圆括号的查询表达式中,并且也应用于外部查询中,则结果是未定义的,可能会在MySQL的未来版本中更改。
Use of column positions is deprecated because the syntax has been removed from the SQL standard.不赞成使用列位置,因为该语法已从SQL标准中删除。
Prior to MySQL 8.0.13, MySQL supported a nonstandard syntax extension that permitted explicit 在MySQL 8.0.13之前,MySQL支持一个非标准的语法扩展,该扩展允许对ASC
or DESC
designators for GROUP BY
columns. GROUP BY
列使用显式ASC
或DESC
指示符。MySQL 8.0.12 and later supports MySQL 8.0.12及更高版本支持ORDER BY
with grouping functions so that use of this extension is no longer necessary. ORDER BY
配合分组函数,因此不再需要使用此扩展。(Bug #86312, Bug #26073525) This also means you can sort on an arbitrary column or columns when using 这也意味着您可以在使用GROUP BY
, like this:GROUP BY
时对任意列进行排序,如下所示:
SELECT a, b, COUNT(c) AS t FROM test_table GROUP BY a,b ORDER BY a,t DESC;
As of MySQL 8.0.13, the 从MySQL 8.0.13开始,不再支持GROUP BY
extension is no longer supported: ASC
or DESC
designators for GROUP BY
columns are not permitted.GROUP BY
扩展:不允许使用ASC
或DESC
指示符来表示GROUP BY
列。
When you use 使用ORDER BY
or GROUP BY
to sort a column in a SELECT
, the server sorts values using only the initial number of bytes indicated by the max_sort_length
system variable.ORDER BY
或GROUP BY
对SELECT
中的列进行排序时,服务器仅使用max_sort_length
系统变量指示的初始字节数对值进行排序。
MySQL extends the use of MySQL扩展了GROUP BY
to permit selecting fields that are not mentioned in the GROUP BY
clause. GROUP BY
的使用,允许选择GROUP BY
子句中没有提到的字段。If you are not getting the results that you expect from your query, please read the description of 如果您没有从查询中得到预期的结果,请阅读第12.20节,“聚合函数”中的GROUP BY
found in Section 12.20, “Aggregate Functions”.GROUP BY
说明。
GROUP BY
permits a WITH ROLLUP
modifier. GROUP BY
允许使用ROLLUP
修饰符。See Section 12.20.2, “GROUP BY Modifiers”.请参阅第12.20.2节,“GROUP BY修饰符”。
Previously, it was not permitted to use 以前,不允许在具有ORDER BY
in a query having a WITH ROLLUP
modifier. WITH ROLLUP
修饰符的查询中使用ORDER BY
。This restriction is lifted as of MySQL 8.0.12. 从MySQL 8.0.12开始,这个限制就被取消了。See Section 12.20.2, “GROUP BY Modifiers”.见第12.20.2节,“按修改器分组”。
The HAVING
clause, like the WHERE
clause, specifies selection conditions. HAVING
子句与WHERE
子句一样,指定了选择条件。The WHERE
clause specifies conditions on columns in the select list, but cannot refer to aggregate functions. WHERE
子句指定了选择列表中列的条件,但不能引用聚合函数。The HAVING
clause specifies conditions on groups, typically formed by the GROUP BY
clause. HAVING
子句指定组的条件,通常由GROUP BY
子句组成。The query result includes only groups satisfying the 查询结果只包括满足HAVING
conditions. HAVING
条件的组。(If no (如果不存在GROUP BY
is present, all rows implicitly form a single aggregate group.)GROUP BY
,则所有行都隐式地形成一个聚合组。)
The HAVING
clause is applied nearly last, just before items are sent to the client, with no optimization. HAVING
子句几乎是最后应用的,就在项目发送到客户机之前,没有进行优化。((LIMIT
is applied after HAVING
.)LIMIT
在HAVING
后应用。)
The SQL standard requires that SQL标准要求HAVING
must reference only columns in the GROUP BY
clause or columns used in aggregate functions. HAVING
必须只引用GROUP BY
子句中的列或聚合函数中使用的列。However, MySQL supports an extension to this behavior, and permits 但是,MySQL支持对这种行为的扩展,并允许HAVING
to refer to columns in the SELECT
list and columns in outer subqueries as well.HAVING
引用SELECT
列表中的列和外部子查询中的列。
If the 如果HAVING
clause refers to a column that is ambiguous, a warning occurs. HAVING
子句引用的列不明确,则会出现警告。In the following statement, 在下面的语句中,col2
is ambiguous because it is used as both an alias and a column name:col2
是不明确的,因为它同时用作别名和列名:
SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;
Preference is given to standard SQL behavior, so if a 首选标准SQL行为,因此如果HAVING
column name is used both in GROUP BY
and as an aliased column in the select column list, preference is given to the column in the GROUP BY
column.HAVING
列名在GROUP BY
和选择列列表中用作别名列,则首选GROUP BY
列中的列。
Do not use 对于应该在HAVING
for items that should be in the WHERE
clause. WHERE
子句中的项,不要使用HAVING
。For example, do not write the following:例如,不要写以下内容:
SELECTcol_name
FROMtbl_name
HAVINGcol_name
> 0;
Write this instead:改为这样写:
SELECTcol_name
FROMtbl_name
WHEREcol_name
> 0;
The HAVING
clause can refer to aggregate functions, which the WHERE
clause cannot:HAVING
子句可以引用聚合函数,WHERE
子句不能:
SELECT user, MAX(salary) FROM users GROUP BY user HAVING MAX(salary) > 10;
(This did not work in some older versions of MySQL.)(这在MySQL的一些旧版本中不起作用。)
MySQL permits duplicate column names. MySQL允许重复的列名。That is, there can be more than one 也就是说,可以有多个具有相同名称的select_expr
with the same name. select_expr
。This is an extension to standard SQL. 这是对标准SQL的扩展。Because MySQL also permits 因为MySQL还允许GROUP BY
and HAVING
to refer to select_expr
values, this can result in an ambiguity:GROUP BY
和HAVING
引用select_expr
值,这可能会导致歧义:
SELECT 12 AS a, a FROM t GROUP BY a;
In that statement, both columns have the name 在该语句中,两列的名称都是a
. a
。To ensure that the correct column is used for grouping, use different names for each 为确保使用正确的列进行分组,请为每个select_expr
.select_expr
使用不同的名称。
The WINDOW
clause, if present, defines named windows that can be referred to by window functions. WINDOW
子句(如果存在)定义了可由窗口函数引用的命名窗口。For details, see Section 12.21.4, “Named Windows”.有关详细信息,请参阅第12.21.4节,“命名窗口”。
MySQL resolves unqualified column or alias references in MySQL在ORDER BY
clauses by searching in the select_expr
values, then in the columns of the tables in the FROM
clause. ORDER BY
子句中解析非限定列或别名引用,方法是先搜索select_expr
值,然后搜索FROM
子句中表的列。For 对于GROUP BY
or HAVING
clauses, it searches the FROM
clause before searching in the select_expr
values. GROUP BY
或HAVING
子句,它先搜索FROM
子句,然后再搜索select_expr
值。(For (对于GROUP BY
and HAVING
, this differs from the pre-MySQL 5.0 behavior that used the same rules as for ORDER BY
.)GROUP BY
和HAVING
,这与MySQL5.0之前的行为不同,后者使用与ORDER BY
相同的规则。)
The LIMIT
clause can be used to constrain the number of rows returned by the SELECT
statement. LIMIT
子句可用于约束SELECT
语句返回的行数。LIMIT
takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:LIMIT
接受一个或两个数值参数,这两个参数都必须是非负整数常量,但有以下例外:
Within prepared statements, 在准备好的语句中,可以使用LIMIT
parameters can be specified using ?
placeholder markers.?
占位符标记。
Within stored programs, 在存储程序中,可以使用整数值例程参数或局部变量指定LIMIT
parameters can be specified using integer-valued routine parameters or local variables.LIMIT
参数。
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. 对于两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。The offset of the initial row is 0 (not 1):初始行的偏移量为0(不是1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. 要检索从某个偏移量到结果集末尾的所有行,可以使用一些大的数字作为第二个参数。This statement retrieves all rows from the 96th row to the last:此语句检索从第96行到最后一行的所有行:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return from the beginning of the result set:对于一个参数,该值指定从结果集开头返回的行数:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, 换句话说,LIMIT
is equivalent to row_count
LIMIT 0,
.row_count
LIMIT
等同于row_count
LIMIT 0,
。row_count
For prepared statements, you can use placeholders. 对于准备好的语句,可以使用占位符。The following statements return one row from the 以下语句从tbl
table:tbl
表返回一行:
SET @a=1; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?'; EXECUTE STMT USING @a;
The following statements return the second to sixth rows from the 以下语句返回tbl
table:tbl
表的第二到第六行:
SET @skip=1; SET @numrows=5; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?'; EXECUTE STMT USING @skip, @numrows;
For compatibility with PostgreSQL, MySQL also supports the 为了与PostgreSQL兼容,MySQL还支持LIMIT
syntax.row_count
OFFSET offset
LIMIT
语法。row_count
OFFSET offset
If 如果LIMIT
occurs within a parenthesized query expression and also is applied in the outer query, the results are undefined and may change in a future version of MySQL.LIMIT
出现在带圆括号的查询表达式中,并且也应用在外部查询中,那么结果将是未定义的,并且可能在MySQL的未来版本中更改。
The SELECT ... INTO
form of SELECT
enables the query result to be written to a file or stored in variables. SELECT
的SELECT ... INTO
的形式可以将查询结果写入文件或存储在变量中。For more information, see Section 13.2.10.1, “SELECT ... INTO Statement”.有关更多信息,请参阅第13.2.10.1节,“SELECT ... INTO语句”。
If you use 如果对使用页锁或行锁的存储引擎使用FOR UPDATE
with a storage engine that uses page or row locks, rows examined by the query are write-locked until the end of the current transaction.FOR UPDATE
,则查询检查的行将被写锁定,直到当前事务结束。
You cannot use 在诸如FOR UPDATE
as part of the SELECT
in a statement such as CREATE TABLE
. new_table
SELECT ... FROM old_table
...CREATE TABLE
之类的语句中,不能将new_table
SELECT ... FROM old_table
...FOR UPDATE
用作SELECT
的一部分。(If you attempt to do so, the statement is rejected with the error Can't update table '(如果您尝试这样做,则会拒绝该语句,理由为Can't update table 'old_table
' while 'new_table
' is being created.)old_table
' while 'new_table
' is being created。)
FOR SHARE
and LOCK IN SHARE MODE
set shared locks that permit other transactions to read the examined rows but not to update or delete them. FOR SHARE
和LOCK IN SHARE MODE
设置共享锁,允许其他事务读取检查的行,但不能更新或删除它们。FOR SHARE
and LOCK IN SHARE MODE
are equivalent. FOR SHARE
和LOCK IN SHARE MODE
是等效的。However, 但是,FOR SHARE
, like FOR UPDATE
, supports NOWAIT
, SKIP LOCKED
, and OF
options. tbl_name
FOR SHARE
和FOR UPDATE
一样,支持NOWAIT
、SKIP LOCKED
和OF
选项。tbl_name
FOR SHARE
is a replacement for LOCK IN SHARE MODE
, but LOCK IN SHARE MODE
remains available for backward compatibility.FOR SHARE
是LOCK IN SHARE MODE
的替代品,但LOCK IN SHARE MODE
仍可用于向后兼容。
NOWAIT
causes a FOR UPDATE
or FOR SHARE
query to execute immediately, returning an error if a row lock cannot be obtained due to a lock held by another transaction.NOWAIT
导致FOR UPDATE
或FOR SHARE
查询立即执行,如果由于另一个事务持有的锁而无法获取行锁,则返回错误。
SKIP LOCKED
causes a FOR UPDATE
or FOR SHARE
query to execute immediately, excluding rows from the result set that are locked by another transaction.SKIP LOCKED
导致FOR UPDATE
或FOR SHARE
查询立即执行,排除结果集中被另一个事务锁定的行。
NOWAIT
and SKIP LOCKED
options are unsafe for statement-based replication.NOWAIT
和SKIP LOCKED
选项对于基于语句的复制不安全。
Queries that skip locked rows return an inconsistent view of the data. 跳过锁定行的查询返回不一致的数据视图。因此,SKIP LOCKED
is therefore not suitable for general transactional work. SKIP LOCKED
不适用于一般事务性工作。However, it may be used to avoid lock contention when multiple sessions access the same queue-like table.但是,当多个会话访问同一个类似队列的表时,它可以用来避免锁争用。
OF
applies tbl_name
FOR UPDATE
and FOR SHARE
queries to named tables. For example:OF
应用于对命名表的tbl_name
FOR UPDATE
和FOR SHARE
查询。例如:
SELECT * FROM t1, t2 FOR SHARE OF t1 FOR UPDATE OF t2;
All tables referenced by the query block are locked when 如果省略了OF
is omitted. tbl_name
OF
,则查询块引用的所有表都将被锁定。tbl_name
Consequently, using a locking clause without 因此,将一个不带OF
in combination with another locking clause returns an error. tbl_name
OF
的锁定子句与另一个锁定子句结合使用会返回错误。tbl_name
Specifying the same table in multiple locking clauses returns an error. 在多个锁定子句中指定同一个表将返回错误。If an alias is specified as the table name in the 如果在SELECT
statement, a locking clause may only use the alias. SELECT
语句中将别名指定为表名,则锁定子句只能使用别名。If the 如果SELECT
statement does not specify an alias explicitly, the locking clause may only specify the actual table name.SELECT
语句没有显式指定别名,那么锁定子句只能指定实际的表名。
For more information about 有关FOR UPDATE
and FOR SHARE
, see Section 15.7.2.4, “Locking Reads”. FOR UPDATE
和FOR SHARE
的更多信息,请参阅第15.7.2.4节,“锁定读取”。For additional information about 有关NOWAIT
and SKIP LOCKED
options, see Locking Read Concurrency with NOWAIT and SKIP LOCKED.NOWAIT
和SKIP LOCKED
选项的更多信息,请参阅使用NOWAIT
和SKIP LOCKED
锁定读取并发。
Following the 在SELECT
keyword, you can use a number of modifiers that affect the operation of the statement. SELECT
关键字之后,可以使用许多影响语句操作的修饰符。HIGH_PRIORITY
, STRAIGHT_JOIN
, and modifiers beginning with SQL_
are MySQL extensions to standard SQL.HIGH_PRIORITY
、STRAIGHT_JOIN
和以SQL_
开头的修饰符是标准SQL的MySQL扩展。
The ALL
and DISTINCT
modifiers specify whether duplicate rows should be returned. ALL
和DISTINCT
修饰符指定是否应返回重复的行。ALL
(the default) specifies that all matching rows should be returned, including duplicates. ALL
(默认值)指定应返回所有匹配的行,包括重复的行。DISTINCT
specifies removal of duplicate rows from the result set. DISTINCT
指定从结果集中删除重复行。It is an error to specify both modifiers. 指定两个修饰符都是错误的。DISTINCTROW
is a synonym for DISTINCT
.DISTINCTROW
是DISTINCT
的同义词。
In MySQL 8.0.12 and later, 在MySQL8.0.12及更高版本中,DISTINCT
can be used with a query that also uses WITH ROLLUP
. DISTINCT
可以与同样使用ROLLUP
的查询一起使用。(Bug #87450, Bug #26640100)
HIGH_PRIORITY
gives the SELECT
higher priority than a statement that updates a table. HIGH_PRIORITY
赋予SELECT
比更新表的语句更高的优先级。You should use this only for queries that are very fast and must be done at once. 您应该仅将其用于非常快速且必须立即完成的查询。A 在锁定表以进行读取时发出的SELECT HIGH_PRIORITY
query that is issued while the table is locked for reading runs even if there is an update statement waiting for the table to be free. SELECT HIGH_PRIORITY
查询,即使有更新语句等待表释放,也会运行该查询。This affects only storage engines that use only table-level locking (such as 这只影响仅使用表级锁定的存储引擎(如MyISAM
, MEMORY
, and MERGE
).MyISAM
、MEMORY
和MERGE
)。
HIGH_PRIORITY
cannot be used with SELECT
statements that are part of a UNION
.HIGH_PRIORITY
不能与属于联合的SELECT
语句一起使用。
STRAIGHT_JOIN
forces the optimizer to join the tables in the order in which they are listed in the FROM
clause. STRAIGHT_JOIN
强制优化器按照表在FROM
子句中列出的顺序连接这些表。You can use this to speed up a query if the optimizer joins the tables in nonoptimal order. 如果优化器以非最佳顺序联接表,则可以使用此选项加快查询速度。在STRAIGHT_JOIN
also can be used in the table_references
list. table_references
列表中也可以使用STRAIGHT_JOIN
。See Section 13.2.10.2, “JOIN Clause”.请参阅第13.2.10.2节,“JOIN子句”。
STRAIGHT_JOIN
does not apply to any table that the optimizer treats as a const
or system
table. STRAIGHT_JOIN
不适用于优化器视为const
表或system
表的任何表。Such a table produces a single row, is read during the optimization phase of query execution, and references to its columns are replaced with the appropriate column values before query execution proceeds. 这样的表生成一行,在查询执行的优化阶段读取,在查询执行继续之前,对其列的引用被替换为适当的列值。These tables appear first in the query plan displayed by 这些表首先出现在EXPLAIN
. EXPLAIN
显示的查询计划中。See Section 8.8.1, “Optimizing Queries with EXPLAIN”. 请参阅第8.8.1节,“使用EXPLAIN优化查询”。This exception may not apply to 此异常可能不适用于在外部联接的const
or system
tables that are used on the NULL
-complemented side of an outer join (that is, the right-side table of a LEFT JOIN
or the left-side table of a RIGHT JOIN
.NULL
补边上使用的const
表或system
表(即,LEFT JOIN
的右侧表或RIGHT JOIN
的左侧表)。
SQL_BIG_RESULT
or SQL_SMALL_RESULT
can be used with GROUP BY
or DISTINCT
to tell the optimizer that the result set has many rows or is small, respectively. SQL_BIG_RESULT
或SQL_SMALL_RESULT
可与GROUP BY
或DISTINCT
一起使用,以告诉优化器结果集分别有多行或小行。For 对于SQL_BIG_RESULT
, MySQL directly uses disk-based temporary tables if they are created, and prefers sorting to using a temporary table with a key on the GROUP BY
elements. SQL_BIG_RESULT
,MySQL直接使用基于磁盘的临时表(如果创建了临时表),并且更喜欢排序,而不是使用在GROUP BY
元素上带有键的临时表。For 对于SQL_SMALL_RESULT
, MySQL uses in-memory temporary tables to store the resulting table instead of using sorting. SQL_SMALL_RESULT
,MySQL使用内存中的临时表来存储生成的表,而不是使用排序。This should not normally be needed.这通常不需要。
SQL_BUFFER_RESULT
forces the result to be put into a temporary table. SQL_BUFFER_RESULT
强制将结果放入临时表中。This helps MySQL free the table locks early and helps in cases where it takes a long time to send the result set to the client. 这有助于MySQL尽早释放表锁,并在需要很长时间才能将结果集发送到客户端的情况下提供帮助。This modifier can be used only for top-level 此修饰符只能用于顶级SELECT
statements, not for subqueries or following UNION
.SELECT
语句,不能用于子查询或后续UNION
。
SQL_CALC_FOUND_ROWS
tells MySQL to calculate how many rows there would be in the result set, disregarding any LIMIT
clause. SQL_CALC_FOUND_ROWS
告诉MySQL计算结果集中将有多少行,而忽略任何LIMIT
子句。The number of rows can then be retrieved with 然后可以使用SELECT FOUND_ROWS()
. SELECT FOUND_rows()
检索行数。See Section 12.16, “Information Functions”.见第12.16节,“信息函数”。
The 从MySQL8.0.17开始,SQL_CALC_FOUND_ROWS
query modifier and accompanying FOUND_ROWS()
function are deprecated as of MySQL 8.0.17; expect them to be removed in a future version of MySQL. SQL_CALC_FOUND_ROWS
查询修饰符和附带的FOUND_ROWS()
函数就不再使用了;希望在MySQL的未来版本中删除它们。See the 有关替代策略的信息,请参阅FOUND_ROWS()
description for information about an alternative strategy.find_ROWS()
的说明。
The 在MySQL 8.0之前的查询缓存中使用了SQL_CACHE
and SQL_NO_CACHE
modifiers were used with the query cache prior to MySQL 8.0. SQL_CACHE
和SQL_NO_CACHE
修饰符。The query cache was removed in MySQL 8.0. 在MySQL 8.0中删除了查询缓存。The SQL_CACHE
modifier was removed as well. SQL_CACHE
修饰符也被删除。SQL_NO_CACHE
is deprecated, and has no effect; expect it to be removed in a future MySQL release.SQL_NO_CACHE
已弃用,并且没有效果;希望在将来的MySQL版本中删除它。