SQL: SELECT的执行顺序

内容来自 极客时间 课程: SQL必知必会的阅读笔记.

SELECT的执行顺序

关键字的顺序

SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY …

SELECT执行顺序(MySQL和Oracle基本相同)

FROM > WHERE > GROUP BY > HAVING > SELECT的字段 > DISTINCT > ORDER BY > LIMIT

MySQL数据库图标
image-2815

执行顺序示例

SELECT DISTINCT player_id, player_name, count(*) as num # 顺序 5
FROM player JOIN team ON player.team_id = team.team_id # 顺序 1
WHERE height > 1.80 # 顺序 2
GROUP BY player.team_id # 顺序 3
HAVING num > 2 # 顺序 4
ORDER BY num DESC # 顺序 6
LIMIT 2 # 顺序 7

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据