用一条SQL语句 查询出每门课都大于80分的学生姓名,表( #test)如下:
Name Course Mark 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 英语 100 王五 语文 81
create table #test
( Name varchar(10), Course varchar(10), Mark float )insert into #test
select '张三', '语文', 81 union select '张三', '数学', 75 union select '李四', '语文', 76 union select '李四', '数学', 90 union select '王五', '英语', 100 union select '王五', '语文', 81方法A select distinct Name
from #test A where not exists(select Course from #test where Mark < 80 and Name = A.Name)方法B
select * from #test a where mark > 80 and (select count(*) from #test where name=a.name)= (select count(*) from #test where name=a.name and mark > 80)
方法C
select distinct name from #test a where not exists(select * from #test where a.name=name and mark<80) (我认为这种较好)
方法D
select distinct name from #test where
name not in ( select name from #test where mark<=80 )方法E
select name,min(mark) from #test group by name having min(mark)>80
-----66666 DECLARE @test table ( Name varchar(10), Course varchar(10), Mark float) insert into @test select '张三 ', '语文 ', 81 union select '张三 ', '数学 ', 75 union select '李四 ', '语文 ', 76 union select '李四 ', '数学 ', 90 union select '王五 ', '英语 ', 100 union select '王五 ', '语文 ', 81 SELECT NAME FROM @test GROUP BY name HAVING count(*)=count(case when mark>=80 then 1 else null end) 或者 SELECT NAME FROM @test GROUP BY name HAVING count(*)=sum(case when mark>=80 then 1 else 0 end)
---方法F
select name from #test group by name having min(mark)>80