How to write SQL statements for such requirements? A sorted SQL statement
Sort by status field in reverse order
If the status = 1 time field is in positive order, if the status = 0 time field is in reverse order
The following are the expected results.
Id status time
5 1 50
6 1 51
1 0 99
10 0 1
Reply to discussion (solution)
Select * from (select * from table where status = 1 order by time) aunion allselect * from (select * from table 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 table where status = 1 order by time) aunion allselect * from (select * from table where status = o order by time desc) B
No, I tried. order will take effect. it must be followed by limit.
Select * from table order by status desc, if (status = 1, 'Time asc ', 'Time desc ');
This method has also been tried. invalid if statements cannot be used like this.
Have you tried it?
What database do you use?
Select * from table order by status desc, if (status = 1, 'Time asc ', 'Time desc ');
This method has also been tried. invalid if statements cannot be used like this.
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
Where
Time * if (status =,-1) changes the time of status = 0 to a negative value to adapt to the overall ascending order.
Status = 1 desc
Order by time (case when status = 1 then desc else asc end)
In this case, an error is reported.
Change to order by (case when status = 1 then 'time desc 'else' time asc 'end)
The test fails. The result is the same as that of union.
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
Where
Time * if (status =,-1) changes the time of status = 0 to a negative value to adapt to the overall ascending order.
Status = 1 desc
This only applies to this table.
Order by time (case when status = 1 then desc else asc end)
In this case, an error is reported.
Change to order by (case when status = 1 then 'time desc 'else' time asc 'end)
The test fails. The result is the same as that of union.
Select * from table order by status = 1 desc, status = 0 asc simple, crude, and practical
select * from tbl_name order by status=1 desc, time*if(status=1,1,-1)
Where
Time * if (status =,-1) changes the time of status = 0 to a negative value to adapt to the overall ascending order.
Status = 1 desc
This only applies to this table.
This is useful, but it is a little inefficient to perform operations on all rows in the IF query. I used to write a function (to implement the function if) and decided to create more fields, desc according to this field after calculation
However, this lacks flexibility. if there are other special sorting tasks, you may need to create fields, and then vertically cut the sorting fields out... (for large data volumes)
Order by time (case when status = 1 then desc else asc end)
In this case, an error is reported.
Change to order by (case when status = 1 then 'time desc 'else' time asc 'end)
The test fails. The result is the same as that of union.
Select * from table order by status = 1 desc, status = 0 asc simple, crude, and practical
This sentence cannot be implemented...
Select * from (select * from table where status = 1 order by time) aunion allselect * from (select * from table where status = o order by time desc) B
No, I tried. order will take effect. it must be followed by limit.
What is your database?
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
I am running OK on mysql 5.6.
Select * from (select * from table where status = 1 order by time) aunion allselect * from (select * from table where status = o order by time desc) B
No, I tried. order will take effect. it must be followed by limit.
What is your database?
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
I am running OK on mysql 5.6.
Mysql 5.5.24