简体   繁体   中英

mysql order by value, merging columns

I have a database of accounts, each account having some sort of rank (Member, Hidden will be used here)

The extent of the query isn't an issue, so I'll just use an english example of what I'm trying to achieve.

SELECT * FROM accounts ... ORDER BY rank (IF rank="Hidden" THEN rank="Member") DESC

This obviously didn't work, but basically if the account rank is hidden, it should be treated as if it was a member rank (hence why it's hidden).

Is this possible to do?

This should do what you want using a simple CASE ;

SELECT * 
FROM accounts 
ORDER BY CASE WHEN rank='Hidden' THEN 'Member' ELSE rank END DESC

A very simple SQLfiddle to test with .

You can use a case statement in the order by to modify ordering:

 ORDER BY
 CASE 
     WHEN rank="Hidden" THEN 1
     WHEN rank="Member" THEN 1
     ELSE 0
 END DESC 

This allows you to rank the user levels as you see fit.

Please see modified SQL Fiddle - http://sqlfiddle.com/#!2/dcc71/5/0

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