How does this need SQL to write?

Source: Internet
Author: User
A sort of SQL
Reverse By Status field
If the Status=1 time field is a positive order, if the status=0 time field is reversed

Here are the results that need to come out

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 or Der by time Desc) b

SELECT * FROM table order by Status Desc,if (Status=1, ' time ASC ', ' time Desc ');

Order by Time (case, 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 or Der by time Desc) b



No, I tried, order is going to work, you have to follow limit.

SELECT * FROM table order by Status Desc,if (Status=1, ' time ASC ', ' time Desc ');


This kind of writing also tried, invalid if not so use

Did you try it?
What database do you use?


SELECT * FROM table order by Status Desc,if (Status=1, ' time ASC ', ' time Desc ');


This kind of writing also tried, invalid if not so use

SELECT * from Tbl_name ORDER BY Status=1 Desc, time*if (status=1,1,-1)

which
Time*if (status=1,1,-1) changes the time of status=0 to a negative number to fit the overall ascending order
Status=1 desc Status=1 All the top of the line

Order by Time (case, status = 1 then desc else ASC End)



That's a direct error.

Change to order by (case-Status = 1 Then ' time desc ' Else ' time ASC ' end)
Test no, same as Union effect

SELECT * from Tbl_name ORDER BY Status=1 Desc, time*if (status=1,1,-1)

which
Time*if (status=1,1,-1) changes the time of status=0 to a negative number to fit the overall ascending order
Status=1 desc Status=1 All the top of the line

This only applies to this table


Order by Time (case, status = 1 then desc else ASC End)



That's a direct error.

Change to order by (case-Status = 1 Then ' time desc ' Else ' time ASC ' end)
Test no, same as Union effect

SELECT * FROM table ORDER BY Status=1 DESC, status=0 asc simple rough and practical


SELECT * from Tbl_name ORDER BY Status=1 Desc, time*if (status=1,1,-1)

which
Time*if (status=1,1,-1) changes the time of status=0 to a negative number to fit the overall ascending order
Status=1 desc Status=1 All the top of the line

This only applies to this table



This is useful, but if query all rows to perform operation and then compare efficiency a bit low, I once wanted to write a function (to implement the function if), and later decided to build more fields, calculated after the field desc
But this lack of flexibility, if there are other special sort may also have to build a field, and then the sorting field is cut out vertically .... (For large data volumes)



Order by Time (case, status = 1 then desc else ASC End)



That's a direct error.

Change to order by (case-Status = 1 Then ' time desc ' Else ' time ASC ' end)
Test no, same as Union effect

SELECT * FROM table ORDER BY Status=1 DESC, status=0 asc simple rough and practical


This sentence can not be realized ...


SELECT * FROM (SELECT * from table where status = 1 order by time) aunion Allselect * FROM (SELECT * from table where status = O or Der by time Desc) b



No, I tried, order is going to work, you have to follow limit.




What kind of database are you?
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 run on MySQL 5.6 is OK



SELECT * FROM (SELECT * from table where status = 1 order by time) aunion Allselect * FROM (SELECT * from table where status = O or Der by time Desc) b



No, I tried, order is going to work, you have to follow limit.




What kind of database are you?
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 run on MySQL 5.6 is OK


MySQL 5.5.24
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.