简体   繁体   中英

Need help with MySQL query

Is it possible to edit the query below:

SELECT *
FROM t1    
ORDER BY CASE
    WHEN projects_status = 'active'  THEN 1
    WHEN projects_status = 'expired' THEN 2
    WHEN projects_status = 'closed'  THEN 3
    END 

to have results in the following sorting order:

  • Active projects_status ASC
  • Expired projects_status DESC
  • Closed projects_status DESC

A comparison will return a 0 or 1, where a 0 is normally sorted before a 1. So when you say ASC, you probably want it the return the rows where this is true (1) on top. So you need to use DESC in this case.

ORDER BY
projects_status = 'active' DESC,
projects_status = 'expired' ASC,
projects_status = 'closed' ASC

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