sql where后面不能使用别名吗
--这个sql语句为什么会错误selecttop2"Temp".*,"D".dname"DeptName","S".grade"SalGrade"from(select"...
--这个sql语句为什么会错误
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
order by "Temp"."avgSal" desc --这里的差别
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
--这个sql语句为什么会正确
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
order by "Temp"."avgSal" desc --这里的差别
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
order by "Temp"."avgSal" desc
//注意:上面两个语句的差别是 order by "avgSal" desc 放在子select末尾还是外部select末尾
错误消息:
消息 1033,级别 15,状态 1,第 10 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 展开
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
order by "Temp"."avgSal" desc --这里的差别
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
--这个sql语句为什么会正确
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
order by "Temp"."avgSal" desc --这里的差别
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
order by "Temp"."avgSal" desc
//注意:上面两个语句的差别是 order by "avgSal" desc 放在子select末尾还是外部select末尾
错误消息:
消息 1033,级别 15,状态 1,第 10 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 展开
4个回答
展开全部
你用了子查询,子查询里是不能用order by的。但是,如果用了TOP,可以用order by。因为,使用order by,可以改变查询结果。如select top 1 * from tn order by id,得到最前一条;select top 1 * from tn order by id desc,得到最后一条
追问
是的,但是我想知道为什么,完整说出即采纳
追答
数据在数据库中存储在数据表的空间中,是有顺序的。子查询得到的结果,只是一个结果而已,加不加order by,对存储没有帮助。而子查询只是为了得到数据,与顺序无关,所不不能使用order by。但是,使用top就不一样了。如:
select top 1 id from tn order by id,得到第一条;select top 1 id from tn order by id desc,得到最后一条。所以,在子查询中,如果不用top,则不能用order by。
还有,就个语句跟where后的别名无关。你的提问可能误导一些人。
展开全部
这就是原因:
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
你的第二个语句中最外层的sql有top,所以能够用 order by
而你的第一个语句,top在最外层,order by 却在内层。
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
你的第二个语句中最外层的sql有top,所以能够用 order by
而你的第一个语句,top在最外层,order by 却在内层。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误信息写的很清楚:错误消息:
消息 1033,级别 15,状态 1,第 10 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
【子查询内是不是能用Order By的】
除非改成:
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select top 100 "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
order by "Temp"."avgSal" desc --这里的差别
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
消息 1033,级别 15,状态 1,第 10 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
【子查询内是不是能用Order By的】
除非改成:
select top 2 "Temp".*, "D".dname "DeptName", "S".grade "SalGrade"
from
(
select top 100 "E".deptno "deptNumber",floor(avg("E".sal)) "avgSal",count(*) "PersonNumber"
from emp "E"
where "E".sal > 2000
group by "E".deptno
order by "Temp"."avgSal" desc --这里的差别
) "Temp"
join salgrade "S" on "Temp"."avgSal" >= "S".losal and "Temp"."avgSal" <= "S".hisal
join dept "D" on "D".deptno = "Temp"."deptNumber"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
where 不能使用别名!
order by "avgSal" desc 放在外面!
子查询里是不能用order by
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询