select top 2 * from Customers where city='London' order by newid()
union
select top 3 * from Customers where city='Bern' order by newid()
看上面这个语句该怎么写?上面的写法是错误的
意图如下:
在顾客表里随机抽两个london的客人和3 个Bern的客人!!
...全文
9112打赏收藏
请大家帮忙看一个SQL语句,关于Union 和Order by
select top 2 * from Customers where city='London' order by newid() union select top 3 * from Customers where city='Bern' order by newid() 看上面这个语句该怎么写?上面的写法是错误的 意图如下: 在顾客表里随机抽两个london的客人和3 个Bern的客人!!
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
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 子句中的项就必须出现在选择列表中
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
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
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
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