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

解决方案 »

  1.   

    这样做是不行的。WHERE只是限定哪些可以显示,而不是限定显示几次。
    如果想要这样的结果,试试下面的语句: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
      

  2.   

    try this,create table #t(id int)insert into #t(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
      

  3.   

    1,首先 table 表中的数据是什么样子的,是不是 1只对应a ,2只对应b 这种形式. 
    2,top 4 为什么连个order by 都没有
      

  4.   

    --1
    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)