您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

查询数据的最大排序问题(只能用一条语句写)_MySQL

2025/6/29 0:48:27发布25次查看
create table hard (qu char (11) ,co char (11) ,je numeric(3, 0))
insert into hard values ('a','1',3)
insert into hard values ('a','2',4)
insert into hard values ('a','4',2)
insert into hard values ('a','6',9)
insert into hard values ('b','1',4)
insert into hard values ('b','2',5)
insert into hard values ('b','3',6)
insert into hard values ('c','3',4)
insert into hard values ('c','6',7)
insert into hard values ('c','2',3)
要求查询出来的结果如下:
qu co je
----------- ----------- -----
a 6 9
a 2 4
b 3 6
b 2 5
c 6 7
c 3 4
就是要按qu分组,每组中取je最大的前2位!!
而且只能用一句sql语句!!!
select * from hard a where (select count(*) from hard b
where a.qu=b.qu and b.je>=a.je)order by qu,je desc
选出一条记录, 然后做循环.
这么写会好懂一些?
select * from hard a where je in (select top 2 je from hard b where a.qu=b.qu order by je)
可以这样写:
select * from hard a where je in (select top 2 je from hard b where a.qu=b.qu order by je desc)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product