本文解释了MySQL在执行语句时的逻辑,帮助更好地理解怎样写出正确的SQL语句。

手写顺序:

SELECT DISTINCT

FROM

JOIN ON

WHERE

GROUP BY

HAVING

ORDER BY

LIMIT

MySQL执行顺序

FROM

ON

JOIN

WHERE

GROUP BY

HAVING

DISTINCT

ORDER BY

LIMIT

f66affaa88fd119677e767d18aaa3b5a.png

where 先执行

image-20210721152001024

应该选D。此处对于SUM(area)的限制应该由having来完成,因为where是在group by之前进行筛选,不知道将哪些area拿来做筛选。

group by

image-20210721171019613

最后应该为group by matchid, mdate

留言

2021-07-21

⬆︎TOP