有两表,留言表m,和用户信息表u,留言表m的字段有mid(自动编号),muid,mcon,其中mid是主键,muid是外键,与用户信息表u的uid关联,用户信息表u的字段有uid,uname,ucon,所有字段都非空。
现想查询留言表的第2个5条留言对应的所有信息,mid,mcon,uid,uname,ucon,对应的sql语句应该是怎样????(如若mid是连续的从1-1000,那么要查询6-10的记录)
想了很久了,都没有想到………………

解决方案 »

  1.   

    --不是说mid是自动编号吗?select top 5 * from 留言表m where mid > 5
      

  2.   

    select m.muid,m.mcon,u.uid,u.uname,u.ucon from m left join u on m.muid=u.uid
    where m.muid in (select top 3 uid from (select top 5 * from u) a order by a.mid)
      

  3.   

    declare @t table(mid int identity(1,1),muid int,mcon int)
    insert into @t
    select 1,15
    union all select 2,32
    union all select 3,32
    union all select 4,32
    union all select 5,32
    union all select 6,32declare @b table(uid int,uname varchar(20),ucon int)
    insert into @b
    select 1,'aa',34
    union all select 2,'bwb',351
    union all  select 3,'bfb',353
    union all  select 4,'bgb',355
    union all  select 5,'bdb',356
    union all  select 6,'bab',358
    select a.muid,a.mcon,b.uid,b.uname,b.ucon from @t a inner join @b b
    on a.muid=b.uid
    where b.uid in (select muid from @t where mid between 2 and 5)
      

  4.   

    如果mid是连续的用 第二个
    如果非连续的用这个select   identity(int) mid,a.* into #temp from @t a
    select a.muid,a.mcon,b.uid,b.uname,b.ucon from #temp a inner join @b b
    on a.muid=b.uid
    where b.uid in (select muid from #temp where mid between 2 and 5)
      

  5.   

    用select top 5 * from 留言表m where mid not in (select top 5 mid from m where....)