有两表,留言表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的记录)
想了很久了,都没有想到………………
现想查询留言表的第2个5条留言对应的所有信息,mid,mcon,uid,uname,ucon,对应的sql语句应该是怎样????(如若mid是连续的从1-1000,那么要查询6-10的记录)
想了很久了,都没有想到………………
where m.muid in (select top 3 uid from (select top 5 * from u) a order by a.mid)
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)
如果非连续的用这个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)