日期:2014-05-18 浏览次数:20697 次
select top 30 * from car where zhanjie=1 ORDER BY NEWID() union select top 20 * from car where zhanjie=2 ORDER BY NEWID() 这样写有错误
SELECT * FROM (select top 30 * from car where zhanjie=1 ORDER BY NEWID()) A UNION ALL SELECT * FROM (select top 20 * from car where zhanjie=2 ORDER BY NEWID()) B
------解决方案--------------------
SELECT * FROM (select top 30 * from car where zhanjie=1 ORDER BY NEWID()) A UNION ALL SELECT * FROM (select top 20 * from car where zhanjie=2 ORDER BY NEWID()) B
------解决方案--------------------
select * from ( select top 30 * from car where zhanjie=1 ORDER BY NEWID() ) t union select * from ( select top 20 * from car where zhanjie=2 ORDER BY NEWID() ) r
------解决方案--------------------
select * from (select top 30 * from car where zhanjie=1 ORDER BY NEWID()) t1 union select * from ( select top 20 * from car where zhanjie=2 ORDER BY NEWID() )t2
------解决方案--------------------
用NEWID()效率太低,大数据量就死跷跷了
建议用TABLESTAMP
或者直接指定
select * from car where id in (rand()*22,.....)
------解决方案--------------------
SELECT * FROM ( select *,row_number() over(partition by zhangjie order by newid()) as rn from car ) A where (zhangjie=2 or zhangjie=1) and rn<31
------解决方案--------------------
SELECT top 30 timu
FROM car
WHERE zhangjie = 1
union
SELECT top 20 timu
FROME car
WHERE zhangjie = 2
不知可否。