在SQL中我们有时候会用到newid()来随机取值,可是有些时候又要二个表或多个表进行连合取值,可是newid()与union不可以连用,否则会报错。哪么怎么办。下面是一个例子。
select * from (
select * from (
select top 3 * from a where type = 1 order by newid()
) as t1
union all
select * from (
select top 3 * from a where type = 2 order by newid()
) as t2
)p
select * from (
select * from (
select top 3 * from a where type = 1 order by newid()
) as t1
union all
select * from (
select top 3 * from a where type = 2 order by newid()
) as t2
)p