select top 3 * from a where type = 1 union select top 2 * from a where type = 1可以实现,
但我想满足type=1的随机取3条数据,满足type=1的取随机2条数据怎么实现?
用select top 3 * from a where type = 1 order by newid() union select top 2 * from a where type = 1 order by newid()
就出错,望高手给点指点,谢谢!
但我想满足type=1的随机取3条数据,满足type=1的取随机2条数据怎么实现?
用select top 3 * from a where type = 1 order by newid() union select top 2 * from a where type = 1 order by newid()
就出错,望高手给点指点,谢谢!
select top 3 * from a where type = 1union select top 2 * from a where type = 1 order by newid()
select top 3 * from a where type = 1 order by newid()
) as t1
union all
select * from (
select top 2 * from a where type = 1 order by newid()
) as t2
select * from (
select top 3 * from a where type = 1 order by newid()
) as t1
union all
select * from (
select top 2 * from a where type = 2 order by newid()
) as t2
如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。
按照楼上的做法,还是报这样的错误,怎么会事啊,还望楼上的高手指点!
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 top 3 * from a where type = 1 order by newid()
) as t1
union all
select * from (
select top 2 * from a where type = 2 order by newid()
) as t2
) as t
select *
from (select top 3 * from a where type = 1 order by newid())a
union all
select *
from (select top 2 * from a where type = 1 order by newid())b
这样为可以了