13.2.10 SELECT Statement语句

13.2.10.1 SELECT ... INTO Statement语句
13.2.10.2 JOIN Clause子句
13.2.10.3 UNION Clause子句
13.2.10.4 Parenthesized Query Expressions带圆括号的查询表达式
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]
    [FROM table_references
      [PARTITION partition_list]]
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
    [HAVING where_condition]
    [WINDOW window_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 OFFSET offset}]
    [into_option]
    [FOR {UPDATE | SHARE}
        [OF tbl_name [, tbl_name] ...]
        [NOWAIT | SKIP LOCKED]
      | LOCK IN SHARE MODE]
    [into_option]
into_option: {
    INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
export_options
  | INTO DUMPFILE 'file_name'
  | INTO var_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语句最常用的子句如下:

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 FROM DUAL if no tables are referenced.如果没有表被引用,MySQL不需要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:术语指定列或表达式,也可以使用*-速记:

The following list provides additional information about other SELECT clauses:以下列表提供了有关其他SELECT子句的其他信息:

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_PRIORITYSTRAIGHT_JOIN和以SQL_开头的修饰符是标准SQL的MySQL扩展。

13.2.10.1 SELECT ... INTO Statement
13.2.10.2 JOIN Clause
13.2.10.3 UNION Clause
13.2.10.4 Parenthesized Query Expressions