一个sql问题

backham2000 2008-07-22 10:34:42
姓名 科目 分数
张三 语文 81
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

sql语句:写出所有科目均大于80的人的名字
...全文
111 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
backham2000 2008-07-28
  • 打赏
  • 举报
回复
select 1 from tb
输出的结果显示:把所有结果行的值至为1;即结果有多少行,显示多少个1

1
1
1
...
测试结果表明:
如果表中有重复数据,
这个的结果集中有重复select name from tb a where not exists(select 1 from tb where score<80 and name=a.name)

用group by 分组是正确的,避免重复数据

select name from tb group by name having min(score)>=80
greymouseyu 2008-07-23
  • 打赏
  • 举报
回复
select name from tb group by name having min(score)>80 and count(subject)=3

这样是说3门功课都有成绩且均在80分以上的人
ws_hgo 2008-07-23
  • 打赏
  • 举报
回复
11楼
没有看清题意
ws_hgo 2008-07-23
  • 打赏
  • 举报
回复

insert Student
select '张三', '语文',81
union all
select'李四','数学', 90
union all
select '王五', '语文',70
union all
select '王五','数学',100
union all
select '王五','英语', 90
select Sname
from Student
group by Sname
having avg(Mark)>80
ojuju10 2008-07-23
  • 打赏
  • 举报
回复
楼主说的是所有的科目都大于80分的人
ojuju10 2008-07-23
  • 打赏
  • 举报
回复



create table #1(name varchar(20),subject varchar(10),score int)
insert into #1 select '张三','语文',81
insert into #1 select '李四','数学',90
insert into #1 select '王五','语文',81
insert into #1 select '王五','数学',100
insert into #1 select '王五','英语',90

select * from #1 a
where score>=80
and exists(select 1 from #1 b
where a.name=b.name
group by b.name
having count(1)=(select count(distinct subject) from #1 c ))
and subject in (select distinct subject from #1)

name subject score
-------------------- ---------- -----------
王五 语文 81
王五 数学 100
王五 英语 90

(3 行受影响)

utpcb 2008-07-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fcuandy 的回复:]
写法比较多,不一一写了。可能手误


SQL code
select name from tb a where not exists(select 1 from tb where score<80 and name=a.name)
select name from tb group by name having count(*)=sum(case when score>=80 then 1 else 0 end)
select name from tb group by name having min(score)>=80
...
[/Quote]
一品梅 2008-07-23
  • 打赏
  • 举报
回复
select 姓名 from tb group by 姓名 having min(分数)>80;
SQLnewlearner 2008-07-23
  • 打赏
  • 举报
回复
create table tb (姓名 varchar(10),科目 varchar(10),分数 int check (分数 between 0 and 100) )

insert tb
select '张三', '语文',81
union all
select'李四','数学', 90
union all
select '王五', '语文',70
union all
select '王五','数学',100
union all
select '王五','英语', 90

select * from tb t1 join (select 姓名,min(分数) mf from tb group by 姓名 )b on b.姓名=t1.姓名 where mf>=80

李四
数学 90 李四 90 张三
语文 81 张三 81
soldierluo 2008-07-22
  • 打赏
  • 举报
回复
select name from dt group by name having min(score)>80
select name from dt where name in(select name from dt where score>80 and name=dt.name) group by name
LYH1235568 2008-07-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hery2002 的回复:]
引用 2 楼 backham2000 的回复:
楼上的兄弟,select 1 from tb, 这1在这句里是什么意思?

表示存在,
随便写个常量都可以替代的
[/Quote]
1指的是该表的第一列属性
hery2002 2008-07-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 backham2000 的回复:]
楼上的兄弟,select 1 from tb, 这1在这句里是什么意思?
[/Quote]
表示存在,
随便写个常量都可以替代的
sdxiong 2008-07-22
  • 打赏
  • 举报
回复
select 姓名
from tableA
group by 姓名
having avg(分数)>80
backham2000 2008-07-22
  • 打赏
  • 举报
回复
楼上的兄弟,select 1 from tb, 这1在这句里是什么意思?
fcuandy 2008-07-22
  • 打赏
  • 举报
回复
写法比较多,不一一写了。可能手误


select name from tb a where not exists(select 1 from tb where score<80 and name=a.name)
select name from tb group by name having count(*)=sum(case when score>=80 then 1 else 0 end)
select name from tb group by name having min(score)>=80
...

34,588

社区成员

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

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