SQL语句的执行顺序是怎么样的????????????????????????

sichuanwww 2006-10-30 12:04:20
把以下的SQL语句的执行顺序按先后顺序排列出来:

1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY

执行顺序是什么样的??哪里有详细的介绍???谢谢!!!考试常被考到,想彻底了解一下!!
...全文
316 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhhdyj 2006-10-30
  • 打赏
  • 举报
回复
用下面的数据测试一下

create table ordertest(type varchar(3), num int)
insert into ordertest
select 'AAA', 1
union all select 'AAA', 3
union all select 'AAA', 5
union all select 'AAA', 7
union all select 'BBB', 1
union all select 'BBB', 3
union all select 'BBB', 5
union all select 'BBB', 7
union all select 'CCC', 1
union all select 'CCC', 3
union all select 'CCC', 5
union all select 'BBB' ,9


select type, sum(num) as num from ordertest
where num 0
group by type
having sum(num) 8

select type, sum(num) as num from ordertest
where num 2
group by type
having sum(num) 8


select type, sum(num) as num from ordertest
where num 2
group by type
having sum(num) 8
order by num desc

drop table ordertest
sichuanwww 2006-10-30
  • 打赏
  • 举报
回复
谢谢!测试先!
hhhdyj 2006-10-30
  • 打赏
  • 举报
回复
SELECT 和 FROM 其实不应该排序,因为他俩是指定选择什么内容,以及从什么地方选择。
选择的时候首先按WHERE中的条件进行数据选择,然后分组,也就是GROUP BY,
然后再执行HAVING条件进一步选择,最后排序。

证据吗,自己写个语句测试一下便知。
出处?我不记得在什么地方见过这样一句话,就是Where的执行优先于Having,
别的就不知道了。

搂主,像这样的问题,实践是最重要的,做个测试一下,一切便很明了
sichuanwww 2006-10-30
  • 打赏
  • 举报
回复
有什么为证???出处在什么地方???希望解释一下!谢谢
hhhdyj 2006-10-30
  • 打赏
  • 举报
回复
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧