1 SELECT DISTINCT <select_list> 2 FROM <left_table> 3 <join_type> JOIN <right_table> 4 ON <join_condition> 5 WHERE <where_condition> 6 GROUP BY <group_by_list> 7 HAVING <having_condition> 8 ORDER BY <order_by_condition> 9 LIMIT <limit_number>
SQL 语句执行顺序:
(7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_type> JOIN <right_table> (2) ON <join_condition> (4) WHERE <where_condition> (5) GROUP BY <group_by_list> (6) HAVING <having_condition> (9) ORDER BY <order_by_condition> (10) LIMIT <limit_number>
|
另外需要注意的问题:
group by 存在时,select中除了聚合函数外,所有的基本列必须是group by里面存在的;having基本上同group by一起使用的,having类似于where语句,只是having过滤是基于group by 分组后的数据,having一般通过select语句里面的聚集函数进行过滤。