SELECT *
FROM (SELECT TOP 10000 *
FROM T
WHERE (state = 'wait')
ORDER BY id DESC) a
UNION ALL
SELECT *
FROM (SELECT TOP 10000 *
FROM T
WHERE (state = 'ok')
ORDER BY id DESC) b
上面的sql语句会把状态是等待的数据先排在前面,状态是完成的数据排在后面显示,已经达到了我的要求,但是这个top蛮不爽的,去掉就会报错,我现在只能把它设置成一个很大的数字,能不能想办法去掉呢?
------解决方案--------------------
- SQL code
select *,PRI=0 from t where state='wait'union allselect *,PRI=1 from t where state='ok'order by pri--或者select * from t order by case when state='wait' then 0 when state='ok' then 1 else 2 end