求一sql 二表关联查询得前3条记录

bxbacn 2009-06-17 04:26:09
TableA

sid sname
1 语文
2 数学
3 英文
4 化学
5 物理
......

TableB

id name fen sid
1 张三 100 1
2 张三 99 2
3 李四 100 1
4 王五 78 3
5 张三 98 3
......

要求得到前3科科目中的各前5名 (也可以是前4科科目中的各前3名)
...全文
214 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallmousel 2009-12-19
  • 打赏
  • 举报
回复
看看,学习一下
lovvver 2009-06-17
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 bxbacn 的回复:]
引用 13 楼 lovvver 的回复:
终极版本,呵呵:

我复制了三次,复制一次一拉下来,还有一回复,放弃复制,在复制第二个,结果还有第三个,呵呵,去试试去先
[/Quote]

非常抱歉,太猴急了点。
bxbacn 2009-06-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lovvver 的回复:]
终极版本,呵呵: [/Quote]

我复制了三次,复制一次一拉下来,还有一回复,放弃复制,在复制第二个,结果还有第三个,呵呵,去试试去先
lchy110 2009-06-17
  • 打赏
  • 举报
回复
NB
lovvver 2009-06-17
  • 打赏
  • 举报
回复
终极版本,呵呵:


--创建测试环境
create table TableA(sid int,sname NVarchar(50));
create table TableB(id int,name NVarchar(20),fen int,sid int);

insert into TableA
select 1,N'语文 '
union all select 2,N'数学'
union all select 3,N'英文'
union all select 4,N'化学'
union all select 5,N'物理'


insert into TableB
select 1,N'张三',100,1
union all select 2,N'张三',99,2
union all select 3,N'李四',100,1
union all select 4,N'王五',78,3
union all select 5,N'张三',98,3
union all select 6,N'aa',99,1
union all select 7,N'x',98,1
union all select 8,N'afda',99,1
union all select 9,N'afea',97,1
union all select 10,N'32s',95,1
union all select 11,N'fff',99,2
union all select 12,N'3w',98,2
union all select 13,N'd',98,2
union all select 14,N'fd',97,2
union all select 15,N'df',96,2
union all select 16,N'ffsf',95,2
union all select 17,N'df',71,3
union all select 18,N'xx',72,3
union all select 19,N'yt',73,3
union all select 20,N'yt',80,4
union all select 21,N'ff',98,5

--查询课程表内前3门课程的前5名学生的分数数据
select a.sname,b.name,b.fen from TableB b,TableA a
where a.sid=b.sid and b.sid in(select top 3 sid from tablea)
and b.fen in(select top 5 fen from TableB where sid=b.sid order by fen desc)
order by b.sid asc,b.fen desc

--清除测试环境
drop table tablea;
drop table tableb;

--执行结果:
/*
sname name fen
语文 张三 100
语文 李四 100
语文 aa 99
语文 afda 99
语文 x 98
数学 张三 99
数学 fff 99
数学 3w 98
数学 d 98
数学 fd 97
英文 张三 98
英文 王五 78
英文 yt 73
英文 xx 72
英文 df 71

*/

lovvver 2009-06-17
  • 打赏
  • 举报
回复
不好意思,落了一个desc:


--创建测试环境
create table TableA(sid int,sname NVarchar(50));
create table TableB(id int,name NVarchar(20),fen int,sid int);

insert into TableA
select 1,N'语文 '
union all select 2,N'数学'
union all select 3,N'英文'
union all select 4,N'化学'
union all select 5,N'物理'


insert into TableB
select 1,N'张三',100,1
union all select 2,N'张三',99,2
union all select 3,N'李四',100,1
union all select 4,N'王五',78,3
union all select 5,N'张三',98,3
union all select 6,N'aa',99,1
union all select 7,N'x',98,1
union all select 8,N'afda',99,1
union all select 9,N'afea',97,1
union all select 10,N'32s',95,1
union all select 11,N'fff',99,2
union all select 12,N'3w',98,2
union all select 13,N'd',98,2
union all select 14,N'fd',97,2
union all select 15,N'df',96,2
union all select 16,N'ffsf',95,2
union all select 17,N'df',71,3
union all select 18,N'xx',72,3
union all select 19,N'yt',73,3
union all select 20,N'yt',80,4
union all select 21,N'ff',98,5

--查询课程表内前3门课程的前5名学生的分数数据
select a.sname,b.name,b.fen from TableB b,TableA a
where a.sid=b.sid and b.sid in(select top 3 sid from tablea)
and b.fen in(select top 5 fen from TableB where sid=b.sid order by fen desc)
order by b.sid asc,b.fen desc

--清除测试环境
drop table tablea;
drop table tableb;

--结果:
/*
sname name fen
语文 aa 99
语文 afda 99
语文 x 98
语文 afea 97
语文 32s 95
数学 3w 98
数学 d 98
数学 fd 97
数学 df 96
数学 ffsf 95
英文 张三 98
英文 王五 78
英文 yt 73
英文 xx 72
英文 df 71

*/


lovvver 2009-06-17
  • 打赏
  • 举报
回复

--创建测试环境
create table TableA(sid int,sname NVarchar(50));
create table TableB(id int,name NVarchar(20),fen int,sid int);

insert into TableA
select 1,N'语文 '
union all select 2,N'数学'
union all select 3,N'英文'
union all select 4,N'化学'
union all select 5,N'物理'


insert into TableB
select 1,N'张三',100,1
union all select 2,N'张三',99,2
union all select 3,N'李四',100,1
union all select 4,N'王五',78,3
union all select 5,N'张三',98,3
union all select 6,N'aa',99,1
union all select 7,N'x',98,1
union all select 8,N'afda',99,1
union all select 9,N'afea',97,1
union all select 10,N'32s',95,1
union all select 11,N'fff',99,2
union all select 12,N'3w',98,2
union all select 13,N'd',98,2
union all select 14,N'fd',97,2
union all select 15,N'df',96,2
union all select 16,N'ffsf',95,2
union all select 17,N'df',71,3
union all select 18,N'xx',72,3
union all select 19,N'yt',73,3
union all select 20,N'yt',80,4
union all select 21,N'ff',98,5

--查询课程表内前3门课程的前5名学生的分数数据
select a.sname,b.name,b.fen from TableB b,TableA a
where a.sid=b.sid and b.sid in(select top 3 sid from tablea)
and b.fen in(select top 5 fen from TableB where sid=b.sid order by fen)
order by b.sid asc,b.fen desc

--清除测试环境
drop table tablea;
drop table tableb;

--结果:
/*
sname name fen
语文 aa 99
语文 afda 99
语文 x 98
语文 afea 97
语文 32s 95
数学 3w 98
数学 d 98
数学 fd 97
数学 df 96
数学 ffsf 95
英文 张三 98
英文 王五 78
英文 yt 73
英文 xx 72
英文 df 71

*/


jinlingoo1 2009-06-17
  • 打赏
  • 举报
回复
都写了,我不写了.提示,必须考虑同分的情况,即并列的..
ljhcy99 2009-06-17
  • 打赏
  • 举报
回复
select B.siid,B.sname,A.name,A.fen,A.c
from t1 as B,
(select *,ROW_NUMBER()over(partition by siid order by fen desc) c
from t2) As A
where A.siid=B.siid
and B.siid<=3
and A.c <=5

c:是排名
bw555 2009-06-17
  • 打赏
  • 举报
回复
字段看串了,修正下
select t2.id,t2.name,t2.fen,t2.sid from tableb t1,tableb t2
where t1.sid=t2.sid and t1.fen<=t2.fen
and sid in (select top 3 sid from tablea)
group by t2.id,t2.name,t2.fen,t2.sid
having count(*)<=5
SQL77 2009-06-17
  • 打赏
  • 举报
回复

SELECT * FROM
(SELECT TOP 5 * FROM
(SELECT id,name,fen,T.SNAME FROM TABLEB B
LEFT JOIN (SELECT TOP 3 * FROM TABLEA)AS T ON B.SID=T.SID)AS T1 WHERE SNAME='语文'
ORDER BY fen DESC)AS T1

UNION ALL

SELECT * FROM
(SELECT TOP 5 * FROM
(SELECT id,name,fen,T.SNAME FROM TABLEB B
LEFT JOIN (SELECT TOP 3 * FROM TABLEA)AS T ON B.SID=T.SID)AS T1 WHERE SNAME=' 数学 '
ORDER BY fen DESC)AS T1

UNION ALL

SELECT * FROM
(SELECT TOP 5 * FROM
(SELECT id,name,fen,T.SNAME FROM TABLEB B
LEFT JOIN (SELECT TOP 3 * FROM TABLEA)AS T ON B.SID=T.SID)AS T1 WHERE SNAME=' 英文 '
ORDER BY fen DESC)AS T1

bw555 2009-06-17
  • 打赏
  • 举报
回复
我考虑了下,排名的话有可能出现并列的情况
没用top的方法获得
[Quote=引用 4 楼 bw555 的回复:]
SQL codeselect t2.id,t2.name,t2.fen,t2.sid from tableb t1,tableb t2
where t1.id=t2.id and t1.fen<=t2.fen
and id in (select top 3 id from tablea)
group by t2.id,t2.name,t2.fen,t2.sid
having count(*)<=5
[/Quote]
SQL77 2009-06-17
  • 打赏
  • 举报
回复
SELECT TOP 5 * FROM (SELECT id,name,fen,T.SNAME FROM TABLEB B LEFT JOIN (SELECT TOP 3 * FROM TABLEA)AS T ON B.SID=T.SID)AS T1 WHERE SNAME='语文' ORDER BY fen DESC

语文前五名!!
bw555 2009-06-17
  • 打赏
  • 举报
回复
select t2.id,t2.name,t2.fen,t2.sid from tableb t1,tableb t2
where t1.id=t2.id and t1.fen<=t2.fen
and id in (select top 3 id from tablea)
group by t2.id,t2.name,t2.fen,t2.sid
having count(*)<=5
yu7733 2009-06-17
  • 打赏
  • 举报
回复
sql 呗
bxbacn 2009-06-17
  • 打赏
  • 举报
回复
ms sql
bw555 2009-06-17
  • 打赏
  • 举报
回复
用的什么数据库?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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