Select * from (select top 2 * from Customers where city='London' order by newid()) a
union 
select * from (select top 3 * from Customers where city='Bern' order by newid()) b

解决方案 »

  1.   

    select * from (select top 2 * from Customers where city='London' order by newid()) tem
    union all
    select * from (select top 3 * from Customers where city='Bern' order by newid()) tem2
      

  2.   

    select * from
    (select top 2 *,newid() as temp_id from syscolumns where name<>'London' order by newid()) a
    union 
    select * from
    (select top 3 *,newid() as temp_id  from syscolumns where name<>'Bern' order by newid()) b
      

  3.   

    select * from (select top 2 * from Customers where city='London' order by newid()) aa
    union all
    select * from (select top 3 * from Customers where city='Bern' order by newid()) bb
      

  4.   

    select coloumnlist (except newid) from (select top 2 *,newid() a from Customers where city='London' order by newid()) tem
    union all
    select coloumnlist (except newid) from (select top 3 *,newid() from Customers where city='Bern' order by newid()) tem2
    otherwise your will get error message
    如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中
      

  5.   

    order by newid()和union无法连用
    try:
    select top 2 * into #temp from Customers where city='London' order by newid()
    insert #temp select top 3 * from Customers where city='Bern' order by newid()
    select * from #temp
    drop table #temp
      

  6.   

    select * from (select top 2 * from Customers where city='London' order by newid()) aunion allselect * from (select top 3 * from Customers where city='Bern' order by newid()) b