sql select语句执行顺序

SQL语句中SELECT是开发或者数据分析中经常要用的,如何写出一个好的SELECT语句,其首要条件就是要知道SELECT的执行顺序,这样才可以更好的为SELECT语句做优化。

SQL的基本结构

一个常见的SELECT SQL结构如下:

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]
 ```

#### SELECT执行顺序
以下步骤显示SELECT语句的逻辑处理顺序或绑定顺序。

1.FROM
2.ON
3.JOIN
4.WHERE
5.GROUP BY
6.WITH CUBE or WITH ROLLUP
7.HAVING
8.SELECT
9.DISTINCT
10.ORDER BY
11.TOP


![](https://www.11meigui.com/wp-content/uploads/2020/01/MySQL-GROUP-BY-Evaluation-Order-300x129.png)
linux scp命令
PHP7 opcache缓存清理问题