比如一张表
create table st(name varchar(10),score int);insert into st
select 'a',98
union all 
select 'b',90
union all 
select 'c',80
union all 
select 'd',80
union all 
select 'e',71
union all 
select 'f',75
union all 
select 'g',80
union all 
select 'h',74写出排名前3的.结果是:
学生   成绩
a        98
b        90
c        80
d        80
g        80
同分排名一样,给出的sql语句为:
select a.* from st a where 3 >(select count(*) from st where score > a.score)
 对于上面的语句我不是很理解,请哪位高手帮我分析下..
特别是  3 >(select count(*) from st where score > a.score);
3可以理解为排名前3的,而 (select count(*) from st where score > a.score)这个的用处是什么.有点迷糊,
请哪位高人指点....

解决方案 »

  1.   

    (select count(*) from st where score > a.score)建议你先自己试着解释一下这个SQL,否则别人也根本不知道你目前已经懂了什么,到什么层次,到底是哪儿不懂。写出你自己的理解,然后让别人来看你的理解是否正确。 否则别人解释过简单你还是理解不了,解释得过细,可能又是根本不必要。
      

  2.   

    主要是我也不理解这个,只是这个sql达到了效果,所以想知道
    select a.* from st a where 3 >(select count(*) from st where score > a.score)
    或则是另有一办法实现这效果吗. 
      

  3.   

    select count(*) from st where score >10 
    能理解吗?如果能,则
    select count(*) from st where score > a.score
    你是如何理解的?如果能,则
    3 >(select count(*) from st where score > a.score)你是如何理解的?
      

  4.   

    select count(*) from st where score > a.score 用数字来比较后,这个我理解了.
    那where 3 是不是就是根据,count(*)出来后的结果来自己添加的呢.
      

  5.   

    假设当前记录 score = 80select count(*) from st where score > a.score
    =
    select count(*) from st where score > 80结果是几? 2!where 3 >(select count(*) from st where score > a.score)
    =
    where 3 >(2)
      

  6.   

    这个按照你之前的方法我做过测试,可以理解;
    就是3(或则是其他数字)的话,是不是就取决于后面count(*)出来的数字,如按照需求count(*)出来的是5,就应该 where 6 ? 是这样吗.