比如有两个表:
news 新闻  newsid   title content之类
message  对新闻评论表:  包括messageid,  content,pubtime,newsid references news(newsid)之类的信息
我要选择news选择10条留言次数最多的从多到少排列这样要怎么写,如果不在news表中添加留言次数字段的情况下

解决方案 »

  1.   

    select * from news a order by (select count(1) from message where newsid=a.newsid) desc
      

  2.   

    select top 10 a.newsid,a.title,a.content,count(1) as 留言数 
    from news a left join message b on a.newsid = b.newsid
    group by a.newsid,a.title,a.content
    order by 留言数
      

  3.   

    先谢谢阿,但是这样有问题阿,我的newsid 1,2,3,4  留言次数分别是 2 1 4 3 按照你的查询出来结果
    是: 3 1 4 2  而正却的应该是  3 1 4 2。。还得请教这是怎么回事呢?
      

  4.   

    不好意思 ,这样是正确的,我写漏了desc  ,谢谢,给分了
      

  5.   

    coolingpipe(冷箫轻笛) 我用的是另外个数据库没有top函数,不过还是谢啦