select a.stu_id,a.sco_id,a.score from score a join score b on(a.sco_id=b.sco_id and a.score<=b.score) group by a.stu_id,a.sco_id,a.score having count(*)<=3
select * from score a where (select count(*) from score where sco_id=a.sco_id and score>=a.score)<=3
create table 成绩表( stu_id varchar(10), sco_id varchar(10), score int )
insert into 成绩表 select 1001,2001,78 union all select 1002,2001,98 union all select 1003,2001,86 union all select 1004,2001,94 union all select 1001,2002,95 union all select 1002,2002,58 union all select 1003,2002,88 union all select 1004,2002,83 union all select 1005,2002,98select sco_id,stu_id,score from 成绩表 a where (select count(*) from 成绩表 where sco_id=a.sco_id and score>=a.score)<=3 drop table 成绩表 _____________________sco_id stu_id score 2001 1002 98 2001 1003 86 2001 1004 94 2002 1001 95 2002 1003 88 2002 1005 98
请问高手:select sco_id,stu_id,score from 成绩表 a where (select count(*) from 成绩表 where sco_id=a.sco_id and score>=a.score)<=3 中的where (select count(*) from 成绩表 where sco_id=a.sco_id and score>=a.score)<=3 是什么意思啊?特别是sco_id=a.sco_id and score>=a.score是什么意思啊?
select stu_id,sco_id,score from score a where (select count(*) from score where sco_id=a.sco_id and score>=a.score)<=3 order by sco_id,score desc
create table 成绩表( stu_id varchar(10), sco_id varchar(10), score int )
insert into 成绩表 select 1001,2001,78 union all select 1002,2001,98 union all select 1003,2001,86 union all select 1004,2001,94 union all select 1001,2002,95 union all select 1002,2002,58 union all select 1003,2002,88 union all select 1004,2002,83 union all select 1005,2002,98 select * from 成绩表 T where stu_id in (select top 3 stu_id from 成绩表 where T.sco_id=sco_id order by score DESC)
select * from score a where (select count(*) from score where sco_id=a.sco_id and score>=a.score)<=3
请问楼上的,select sco_id,stu_id,score from 成绩表 a where (select count(*) from 成绩表 where sco_id=a.sco_id and score>=a.score)<=3 中的where (select count(*) from 成绩表 where sco_id=a.sco_id and score>=a.score)<=3 的详细处理过程是怎么样的?
语句里面可以使用group by 课程ID的方式SQL: select top 3 * from 成绩表 order by 成绩 desc group by 课程ID楼上的那些方法真的是最好的么?试验下我的方法吧
不雅吝啬使用group by 啊!这个才是提高数据库处理能力的最佳方式!
li1229363() ( ) 信誉:100 Blog 2007-03-31 09:36:26 得分: 0
语句里面可以使用group by 课程ID的方式SQL: select top 3 * from 成绩表 order by 成绩 desc group by 课程ID楼上的那些方法真的是最好的么?试验下我的方法吧
on(a.sco_id=b.sco_id and a.score<=b.score) group by a.stu_id,a.sco_id,a.score
having count(*)<=3
stu_id varchar(10),
sco_id varchar(10),
score int
)
insert into 成绩表 select 1001,2001,78
union all select 1002,2001,98
union all select 1003,2001,86
union all select 1004,2001,94
union all select 1001,2002,95
union all select 1002,2002,58
union all select 1003,2002,88
union all select 1004,2002,83
union all select 1005,2002,98select sco_id,stu_id,score from 成绩表 a where (select count(*) from 成绩表 where
sco_id=a.sco_id and score>=a.score)<=3 drop table 成绩表
_____________________sco_id stu_id score
2001 1002 98
2001 1003 86
2001 1004 94
2002 1001 95
2002 1003 88
2002 1005 98
sco_id=a.sco_id and score>=a.score)<=3
中的where (select count(*) from 成绩表 where
sco_id=a.sco_id and score>=a.score)<=3 是什么意思啊?特别是sco_id=a.sco_id and score>=a.score是什么意思啊?
from score a
where (select count(*) from score where sco_id=a.sco_id and score>=a.score)<=3
order by sco_id,score desc
stu_id varchar(10),
sco_id varchar(10),
score int
)
insert into 成绩表 select 1001,2001,78
union all select 1002,2001,98
union all select 1003,2001,86
union all select 1004,2001,94
union all select 1001,2002,95
union all select 1002,2002,58
union all select 1003,2002,88
union all select 1004,2002,83
union all select 1005,2002,98
select * from 成绩表 T where stu_id in (select top 3 stu_id from 成绩表 where T.sco_id=sco_id order by score DESC)
请问楼上的,select sco_id,stu_id,score from 成绩表 a where (select count(*) from 成绩表 where
sco_id=a.sco_id and score>=a.score)<=3
中的where (select count(*) from 成绩表 where
sco_id=a.sco_id and score>=a.score)<=3
的详细处理过程是怎么样的?
语句里面可以使用group by 课程ID的方式SQL: select top 3 * from 成绩表 order by 成绩 desc group by 课程ID楼上的那些方法真的是最好的么?试验下我的方法吧
----------
關鍵問題在於,你的語句得到的結果是不正確的。樓主要的是“每一门课程前3名的成绩.”,而你的結果總共才3條紀錄。
列 'shop_pd_info.pdname' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdjj' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdtime' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdprice' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdhyprice' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pddj' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdimg' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdkc' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdcontent' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.id' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.ispl' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pd_lb_self' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.zysx' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.addtime' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.click' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.isstop' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.orderby' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.slc' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdtype' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdlb' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.new' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.dds' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.istj' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.issh' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.del_time' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.gq' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdtype1' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.pdarea' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'shop_pd_info.dw' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。