有新闻表如下
新闻ID,新闻标题,新闻类别(int),发布时间现在希望取出各类别下的一条新闻,根据发布时间降序

解决方案 »

  1.   

    select * from 新闻表 a 
    where not exists(select 1 from 新闻表 where 新闻类别=a.新闻类别 and 发布时间>a.发布时间 )
    order by 新闻类别 asc--求出各类最新一条信息
      

  2.   

    select * from 新闻表 a where 
      新闻id=(select top1 新闻id from 新闻表 where 新闻标题=a.新闻标题 order by 发布时间 desc)
      

  3.   


    select * from 新闻表 as tmp
    where not exists(select 1 from 新闻表 where 新闻类别=tmp.新闻类别 and 发布时间>tmp.发布时间)
      

  4.   

    select a.* from 新闻表 a,
    (select 新闻类别 , max(发布时间) as 发布时间 from 新闻表 group by 新闻类别) b
    where a.新闻类别 = b.新闻类别 and a.发布时间 = b.发布时间
      

  5.   

    select * from 新闻表 as awhere not exists(select 1 from 新闻表 where 新闻类别=a.新闻类别 and 发布时间>a.发布时间)
      

  6.   

    select a.* from a,(SELECT MAX(新闻ID) AS d, 新闻类别 from a
    GROUP BY 新闻类别) as temp1
     where a.新闻ID=temp1.d order by 发布时间最正确,最好的做法
      

  7.   

    谢谢个位,个人感觉还是这样写比较好
    select * from 新闻 where 新闻ID in (select max(新闻ID) from 新闻 group by 类别)
    简单易理解