如图,摘录了部分数据,要求是查询表table_info中state<30的记录,结果中优先显示state=11的记录,其它的按time排序。
刚才看到还可以通过自定义排序规则来解决这个问题:
select * from table_info order by field(state,'11','20','10'),time desc
select * from table where state=11
union all
select * from table where state<30 and state != 11
select * from table where state=11
union all
(select * from table where state<30 and state != 11
order by time);
这是升序的 语句 如果要降序 IF(state=11,0,1) 适当调整下即可
SELECT * FROM table_info ORDER BY time , IF(state=11,0,1)