id  userid  title   publishdate     
0   1000    123     2007-4-16 9:27
1   1001    456     2007-4-17 9:27
2   1002    789     2007-4-18 9:27
3   1000    789     2007-4-19 9:27
4   1000    312     2007-4-19 10:27想查询到最近发表过的前三个人,按publishdate降序排序id  userid  title   publishdate     
4   1000    312     2007-4-19 10:27
2   1002    789     2007-4-18 9:27
1   1001    456     2007-4-17 9:27

解决方案 »

  1.   

    参考:http://community.csdn.net/Expert/topic/5471/5471507.xml?temp=4.035586E-02
      

  2.   

    select top 3 * from tb order by publishdate desc
      

  3.   

    select top 3
    from 表名
    order by  publishdate
      

  4.   

    Select
    A.*
    From
    TableName A
    Where Not Exists(Select userid From TableName Where userid = A.userid And publishdate > A.publishdate)
    Order By publishdate Desc
      

  5.   

    掉了TOP 3Select
    TOP 3 
    A.*
    From
    TableName A
    Where Not Exists(Select userid From TableName Where userid = A.userid And publishdate > A.publishdate)
    Order By publishdate Desc
      

  6.   

    Select
    TOP 3 
    A.*
    From
    TableName A
    Where publishdate In (Select Max(publishdate) From TableName Where userid = A.userid)
    Order By publishdate Desc
      

  7.   

    Select
    TOP 3 
    A.*
    From
    TableName A
    Inner Join
    (Select userid, Max(publishdate) As publishdate From TableName Group By userid)
    B
    On A.userid = B.userid And A.publishdate = B.publishdate
    Order By A.publishdate Desc
      

  8.   

    --方法一:
    Select
    TOP 3 
    A.*
    From
    TableName A
    Where Not Exists(Select userid From TableName Where userid = A.userid And publishdate > A.publishdate)
    Order By publishdate Desc--方法二:
    Select
    TOP 3 
    A.*
    From
    TableName A
    Where publishdate In (Select Max(publishdate) From TableName Where userid = A.userid)
    Order By publishdate Desc--方法三:
    Select
    TOP 3 
    A.*
    From
    TableName A
    Inner Join
    (Select userid, Max(publishdate) As publishdate From TableName Group By userid)
    B
    On A.userid = B.userid And A.publishdate = B.publishdate
    Order By A.publishdate Desc
      

  9.   

    select top 3 * from Table order by publishdate desc
      

  10.   

    直接用TOP肯定是得不到樓主要的結果的。
      

  11.   

    declare @ta table(id int,  userid int, title int,  publishdate smalldatetime  )  
    insert @ta select 0,   1000,    123,     '2007-4-16 9:27'
    insert @ta select 1,   1001,    456,     '2007-4-17 9:27'
    insert @ta select 2,   1002,    789,     '2007-4-18 9:27'
    insert @ta select 3,   1000,    789,     '2007-4-19 9:27'
    insert @ta select 4,   1000,    312,     '2007-4-19 10:27'
    select * 
    from @ta a
    where 
    (select count(distinct title) from @ta where  id>a.id)<3
    and not exists
    (select 1 from @ta where title=a.title and id<a.id)
    order by id desc
    (1 行受影响)(1 行受影响)(1 行受影响)(1 行受影响)(1 行受影响)
    id          userid      title       publishdate
    ----------- ----------- ----------- -----------------------
    4           1000        312         2007-04-19 10:27:00
    2           1002        789         2007-04-18 09:27:00
    1           1001        456         2007-04-17 09:27:00(3 行受影响)
      

  12.   

    按照樓主的意思,應該不用考慮到title,只需要考慮userid即可。
      

  13.   

    测试过,可以用select b.id,a.userid,b.title,a.publishdate from 
    (select top 3 userid,max(publishdate) publishdate from tb5 group by userid) a 
    left join tb5 b on a.userid=b.userid and a.publishdate=b.publishdate order by a.publishdate desc
      

  14.   

    select * from table1 a where publishdate in (select top 1 publishdate from table1 b where a.userid=b.userid order by publishdate desc) order by publishdate desc
      

  15.   

    select top 3 * from tb order by publishdate desc
      

  16.   

    declare @ta table(id int,  userid int, title int,  publishdate smalldatetime  )  
    insert @ta select 0,   1000,    123,     '2007-4-16 9:27'
    insert @ta select 1,   1001,    456,     '2007-4-17 9:27'
    insert @ta select 2,   1002,    789,     '2007-4-18 9:27'
    insert @ta select 3,   1000,    789,     '2007-4-19 9:27'
    insert @ta select 4,   1000,    312,     '2007-4-19 10:27'
     
    select a.*from @ta a join (select top 3  userid, max(publishdate)publishdate from @ta group by userid) b
                   on a.userid=b.userid and a.publishdate=b.publishdate order by a.publishdate
      

  17.   

    create table #(id int,userid int ,title int,publishdate datetime)
    insert into # values(0,1000,123,'2007-4-16 9:27') 
    insert into # values(1,1001,456,'2007-4-17 9:27')
    insert into # values(2,1002,789,'2007-4-18 9:27')
    insert into # values(3,1000,789,'2007-4-19 9:27')
    insert into # values(4,1000,312,'2007-4-19 10:27')select top 3  *  from # a
    where not exists(select 1 from # where a.userid=userid and a.publishdate<publishdate )
    order by publishdate desc 
    id          userid      title       publishdate                                            
    ----------- ----------- ----------- ------------------------------------------------------ 
    4           1000        312         2007-04-19 10:27:00.000
    2           1002        789         2007-04-18 09:27:00.000
    1           1001        456         2007-04-17 09:27:00.000(所影响的行数为 3 行)
      

  18.   

    幾種方法中,相對來說,用關聯的效率更優一些--方法三:
    Select
    TOP 3 
    A.*
    From
    TableName A
    Inner Join
    (Select userid, Max(publishdate) As publishdate From TableName Group By userid)
    B
    On A.userid = B.userid And A.publishdate = B.publishdate
    Order By A.publishdate Desc
      

  19.   

    用JOIN  ON 的效率稍微的高点 
      

  20.   

    create table userCount(id int,userid int ,title int,publishdate datetime)
    insert into userCount values(0,1000,123,'2007-4-16 9:27') 
    insert into userCount values(1,1001,456,'2007-4-17 9:27')
    insert into userCount values(2,1002,789,'2007-4-18 9:27')
    insert into userCount values(3,1000,789,'2007-4-19 9:27')
    insert into userCount values(4,1000,312,'2007-4-19 10:27')select  top 3 * from userCount as userCount1
    join (select userid,max(publishdate) as publishdate  from userCount group by userid ) userCount3
    on userCount1.userid = userCount3.userid 
    and userCount1.publishdate = userCount3.publishdate 
    order by userCount1.publishdate desc