sql执行顺序
Posted chendeming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql执行顺序相关的知识,希望对你有一定的参考价值。
执行顺序:
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 ...] [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 OUTFILE \'file_name\' [CHARACTER SET charset_name] export_options | INTO DUMPFILE \'file_name\' | INTO var_name [, var_name]] [FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED] | LOCK IN SHARE MODE]]
一、sql执行顺序
(1) from
(2) join
(3) on
(4) where
(5) group by(开始使用select中的别名,后面的语句中都可以使用)
(6) avg,sum……(聚合函数)
(7) having
(8) select
(9) distinct
(10) order by
(11) limit
SELECT语句执行顺序
开始->FROM子句->WHERE子句->GROUP BY子句->HAVING子句->ORDER BY子句->SELECT子句->LIMIT子句->最终结果
每执行一步,就会在内存或者间接存储(临时文件)中,产生一个逻辑表(中间结果),供接下来的子句使用,如果不存在某个子句,就跳过 。
更多文档
https://www.cnblogs.com/clsn/p/8047028.html#auto_id_14
https://www.cnblogs.com/yyjie/p/7788428.html
以上是关于sql执行顺序的主要内容,如果未能解决你的问题,请参考以下文章