我有一个news数据表,里面有个字段mid,我要求一条sql查询,查询出mid=100,mid=200,mid=204,mid=114数据的各前10条数据
最终应该显示的是40条数据,
前10条是mid=100的
11-20条是mid=200的……
一条sql查询能实现吗?怎么写!
------解决方案--------------------
select *
from(select mid,id=row_number()over(partition by mid order by getdate())
from news
where mid in (100,200,204,114))a
where id<11
order by mind,id
------解决方案--------------------
select top(10) * from test
where mid = 100
union all
select top(10) * from test
where mid = 200
union all
select top(10) * from test
where mid = 114
union all
select top(10) * from test
where mid = 204
------解决方案--------------------
select b.* from tb as a
cross apply
(select top 10 * from tb where mid=a.mid and mid in(100,200,204,114) order by 排序列) as b