Maison > Article > développement back-end > 这样的需求sql怎么写?
一个排序的sql
按status 字段倒序
如果status=1 time字段正序,如果status=0 time字段倒序
下面是需要出来的结果
id status time
5 1 50
6 1 51
1 0 99
10 0 1
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
select * from table order by status desc,if(status=1,'time asc','time desc');
order by time (case when status = 1 then desc else asc end)
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
select * from table order by status desc,if(status=1,'time asc','time desc');
去试了没?
你用的什么数据库?
select * from table order by status desc,if(status=1,'time asc','time desc');
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
order by time (case when status = 1 then desc else asc end)
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
order by time (case when status = 1 then desc else asc end)
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
order by time (case when status = 1 then desc else asc end)
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
SELECT * FROM (SELECT * FROM `table2` WHERE `status`=1 ORDER BY time ASC) aUNION ALLSELECT * FROM (SELECT * FROM `table2` WHERE `status`=0 ORDER BY time DESC) b
select * from (select * from 表 where status = 1 order by time)aunion allselect * from (select * from 表 where status = o order by time desc)b
SELECT * FROM (SELECT * FROM `table2` WHERE `status`=1 ORDER BY time ASC) aUNION ALLSELECT * FROM (SELECT * FROM `table2` WHERE `status`=0 ORDER BY time DESC) b