简体   繁体   中英

(MySQL) OrderBy Field1=3, Field2

I wish to order a table:

Firstly by Field1=3 Then by Field2 DESC

I know I can't write OrderBy Field1=3, Field2 DESC

So how can I implement this??

TO CLARIFY:

Let's say I have a table of books. I wish to list ALL the books in the table. I wish the books from 1990 to appear at the top, then the rest of the books in alphabetical order of title.

Actually, you can write the statement you said you can't. Using your clarification example:

SELECT * FROM Books ORDER BY (year = 1990) DESC, name

"year = 1990" will be "1" for ones where year is 1990, so those will go at the top.

This is TSQL rather than MySQL, but it should give you the idea...

(Assuming I understand your question...)

ORDER BY
    CASE WHEN Field1 = 3 THEN 0 ELSE 1 END    ASC,
    Field2                                   DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM