select top 4 * 
from 表 a
where id=(select max(id) from 表 where pid=a.pid)
order by id desc

解决方案 »

  1.   

    select *
    from tablename a
    where id in(select top 2 id from tablename where pid=a.pid order by id desc)
    order by pid asc,id desc
      

  2.   

    select *
    from tablename a
    where id in(select top 2 id from tablename where pid=a.pid order by id desc)
    order by pid asc,id desc
      

  3.   

    select top 4 a.* 
    from 表 a
    where a.id=(select max(id) from 表 where pid=a.pid)
    order by a.id desc
      

  4.   

    select  *  from 表  a where id  in(select  top 2 id from  表  where pid=a.pid  order  by  id desc)
    order  by pid,id desc
      

  5.   

    select  *  from 表  a where id  in(select  top 2 id from  表  where pid=a.pid  order  by  id desc)
    order  by pid,id desc
      

  6.   

    那要是读取不同pid的前2条怎么写?谢谢!~
      

  7.   

    select top 4 a.* 
    from 表 a
    where (select count(*) from 表 where pid=a.pid and id<=a.id)<=前几条
    order by a.id desc
      

  8.   

    愁死了!~请问有什么好的复杂sql语句生成工具?
      

  9.   

    那要是读取不同pid的前2条怎么写?谢谢!~
    select  *  from 表  a where id  in(select  top 2 min(id) from  表  where pid=a.pid  group  by  pid  having count(pid)=1)
    order  by pid,id desc
      

  10.   

    select * top 4 from a
    (select id,distinct pid,text from b1) a 
    order by id desc