select top 4 id,name from table where id in(1,2,3,4)
id 是 自增
这里4个不同id则可以列出4条信息若遇到这种情况,问题来了
select top 4 id,name from table where id in(1,1,2,3)
这样只能得到id name
1 a
2 b
3 c
我想得到 id name
1 a
1 a
2 b
3 c
同理
select top 4 id,name from table where id in(1,1,1,1)
怎么得到id name
1 a
1 a
1 a
1 a
id 是 自增
这里4个不同id则可以列出4条信息若遇到这种情况,问题来了
select top 4 id,name from table where id in(1,1,2,3)
这样只能得到id name
1 a
2 b
3 c
我想得到 id name
1 a
1 a
2 b
3 c
同理
select top 4 id,name from table where id in(1,1,1,1)
怎么得到id name
1 a
1 a
1 a
1 a
如果想要这样的结果,试试下面的语句:select b.id, b.name from
(select 1 id union all select 1 union all select 2 union all select 3) a
left join table b on a.id=b.id
select 1 union all
select 1 union all
select 2 union all
select 3
select top 4 a.id, b.name
from #t a
inner join [table] b on a.id=b.id
2,top 4 为什么连个order by 都没有
select top 4 id,name from table where id in(1,1,2,3)--用以下實現:
select id,name from table where id in(1)
union all
select id,name from table where id in(1,2,3)--2
select top 4 id,name from table where id in(1,1,1,1)--用以下實現:
select id,name from table where id in(1)
union all
select id,name from table where id in(1)
union all
select id,name from table where id in(1)
union all
select id,name from table where id in(1)