在oracle里面有张新闻评论表如下:主键评论信息id    每条新闻的id    评论的标题   评论的内容  
com_id             key_id          com_title   com_content   
1                   3              title1      content1 
2                   3              title2      content2
3                   3              title3      content3
4                   7              title4      content4
5                   7              title5      content5
6                   9              title6      content6
7                   9              title7      content7
8                   9              title8      content8
9                   9              title9      content9
10                  8              title10     content10
11                  8              title11     content11
12                  8              title12     content12
13                  8              title13     content13
14                  8              title14     content14
15                  8              title15     content15这里有15条评论信息,记录了对key_id为3,7,9,8新闻的评论信息,现在我要求前3条评论信息最多的新闻key_id,查出来的结果应该是:key_id
8        //它有6条评论
9        //它有4条评论
3        //它有3条评论

解决方案 »

  1.   

    select key_id from(
      select key_id,count(1) c from tab1
        group by key_id
        order by c desc) 
      where  rowid<=3
      

  2.   

    晕,写错了
    select key_id from(
      select key_id,count(1) c from tab1
        group by key_id
        order by c desc) 
      where  rownum<=3
      

  3.   

    感觉count(*)应该是可以的,就不知道count(1)里面的1是什么意思?
      

  4.   


    count(*)效果是一样的
    count(1)里面的1可以用任意数字或字符串代替。
    每行记录都返回一个1
    count会计算'1'的个数,也就是记录的条数
      

  5.   

    select key_id from(select key_id,count(com_id) count_id from t1 group by key_id order by count_id desc) where rownum<=3