sql语句

wangjia1991 2014-07-25 12:21:46
select * from t_student
order by sage desc
having sage>avg(sage)
出现提示:消息 156,级别 15,状态 1,第 3 行关键字 'having' 附近有语法错误。

查询出年龄大于平均年龄,并按降序的排序方式 的所有学生信息。用上面的方法为什么不行,在不用where的情况下可以解决吗
...全文
112 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangjia1991 2014-07-28
  • 打赏
  • 举报
回复
谢谢,我明白了。
天堂的鸽子 2014-07-25
  • 打赏
  • 举报
回复
select * from t_student WHERE sage>(SELECT avg(sage) FROM t_student) order by sage desc
天堂的鸽子 2014-07-25
  • 打赏
  • 举报
回复
having sage>avg(sage)没有这个写法
wangjia1991 2014-07-25
  • 打赏
  • 举报
回复
上面的t_student(sno,sname,sage,sex,....)
唐诗三百首 2014-07-25
  • 打赏
  • 举报
回复

select *
 from t_student
 where sage>(select avg(sage) from t_student)
 order by sage desc
Neo_whl 2014-07-25
  • 打赏
  • 举报
回复
引用
没有 Group By 时 Having 等同与 Where。 所以可以用,但是容易有歧义,真心不建议用。 楼主的语法错误等于是先有 Order By 再有 Where。
+1
霜寒月冷 2014-07-25
  • 打赏
  • 举报
回复
给你个测试例子
if object_id('[tb]') is not null drop table [tb]
go
create table tb (age int)
insert into tb
select '25' union all
select '30' union all
select '30' union all
select '54' union all
select '55' 
go
select * from tb where age >(select avg(age) from tb) 
go
---取年龄大于平均年龄的
--age
--54
--55
select * from tb a where exists ( 
select 1 from (select age from tb group by age having count(age)=2)
b where a.age=b.age)
---取年龄相同年龄
--age
--30
--30
Tiger_Zhao 2014-07-25
  • 打赏
  • 举报
回复
没有 Group By 时 Having 等同与 Where。
所以可以用,但是容易有歧义,真心不建议用。

楼主的语法错误等于是先有 Order By 再有 Where。
發糞塗牆 2014-07-25
  • 打赏
  • 举报
回复
我记得没group by是不能用having的
hzybc 2014-07-25
  • 打赏
  • 举报
回复
应该有 Group by

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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