日期:2014-05-18  浏览次数:20498 次

mysql中 针对某个字段随机排序+正常排序的组合问题....求数据库高手指点啊
假如数据库中有100条数据,有个字段为state,而state字段只有1,2两种数据,假如现在有50条state字段为1的数据,50条state字段为2的数据,如何通过sql语句得到 对state为1的50条数据随机排序,对state为2的50条数据随机排序,然后再根据state字段排序,state的为1的50条随机数据排在前面,state为2的50条数据排在后面...

------解决方案--------------------
select * from 
(select * from tb where state=1 order by newid()) a
union all
select * from 
(select * from tb where state=2 order by newid()) b
------解决方案--------------------
select * from
(select * from tb where state=1 order by newid()) a
union all
select * from
(select * from tb where state=2 order by newid()) b