我希望根据某列的数值返回前n行数据
举个例子,下面的表格我希望返回拥有count最大的前3个字母。改如何写SQL语句??
word    count
A 12
B 4
C 15
… …
X 20帮个忙,谢谢

解决方案 »

  1.   

    select * from table1 order by `count` desc limit 3;
      

  2.   

    peter_sam_yin (peter_sam_yin)
      '截至2011-09-01 07:38:13  用户结帖率0.00%  当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖
      

  3.   

    select * from tt order by `count` desc limit 3
      

  4.   

    另外这个只是返回前3个,但是如果我希望相同数字的话只算一个呢?比如:
    word count
    A 12
    B 4
    C 15
    D 15
    E 15
    … …
    X 20我希望返回E,C,D,E,A
      

  5.   


    select * from table1 a where 3>(Select count(*) From table1 where word.a.word and `count`>a.`count`);
      

  6.   


    这句话的语法是不是有问题???没看到table1后面的 a 是什么意思?还有word.a.word 理解不了。
      

  7.   

    也许是我举得这个例子的属性名不太好。改一下吧。还是叫table1
    word    w_count
    a 10
    b 13
    c 10
    d 5
    e 2
    f 10
      

  8.   


    mysql> select * from TE a where 3>(Select count(*) From TE where word.a.word and
     `count`>a.`count`);
    ERROR 1054 (42S22): Unknown column 'word.a.word' in 'where clause'
    TE是这个表的表名,数据和我上一回帖贴出来的一样
      

  9.   

    忘了说,第二贴的列名还是之前的count
      

  10.   

    select * from TE a where 3>(Select count(*) From TE where word=a.word and
      `count`>a.`count`);
      

  11.   


    谢谢,可以实现。顺便问问这个到底是什么语法??我没见过。where 前面的a是起到什么作用?
      

  12.   

    A是TE表别名
    select a.word,count(*) from TE a  left join te b on b.word=a.word and b.`count`>a.`count`
    group by a.word
    看看结果就知道了
      

  13.   

    不好意思....后来我查了一下,好像结果不对呀!!!
    word   count
    a 10
    b 13
    c 10
    d 9
    e 2
    f 10
    g 9其结果和原表一样...