22,210
社区成员
发帖
与我相关
我的任务
分享
/*exists 和select 1的用法解释:
exist 即存在。
select 1 from table
与
select * from table 作用上来说是没有差别的,都是查看是否有记录,一般是作条件用的。
select 1 from 中的1是一常量,查到的所有行的值都是它,但从效率上来说,1>*,因为不用查字典表。
*/
/*
编号 类别 名称 分数
1 语文 A 90
2 语文 B 80
3 数学 C 70
输出结果:
1 语文 A 90
3 数学 C 70
请问各位大大,这个SQL应该如何写的
*/
create table #t (编号 int, 类别 varchar(20), 名称 varchar(20), 分数 int)
insert into #t
select 1,'语文','A',90 union
select 2,'语文','B',80 union
select 3,'数学','C',70
select a.*
from #t a
join (select 类别,max(分数) 分数 from #t b group by 类别) b on a.类别=b.类别 and a.分数=b.分数
编号 类别 名称 分数
1 语文 A 90
3 数学 C 70
drop table #t
select * from tb where 编号 in (select min(编号) from tb group by 类别)
---or:
select * from tb t where not exists(select 1 from tb where 类别=t.类别 and 编号<t.编号)
select * from tb a join (select 类别,max(分类) as 分数 from tb group by 类别) b on a.类别=b.类别 and a.分数=b.分数
select *
from tb
where id in
(
select 类别,min(编号)
from tb
group by 类别
)
select * from TAB a,(select id,max(分数) from Tab group by id) b where a.id=b.id
select 编号,类别,名称,max(分类) as 分类 from tb group by 类别
select * from (select id,max(分数) from Tab group by id) b