简体   繁体   中英

MySQL / PDO sorting issue

I have a MySQL string that ends as follows: ORDER BY business_name ASC here the business names are sorted in alphabetic order. Now I have to distinguish between business that are verified and those that aren't. There are 3 levels of verification these are the unverified, bronze, silver and gold.

I want to display all values using MySQL but how can I display them sorted by first gold, second silver, third bronze and finally fourth unverified. I want each category of gold, silver, bronze and unverified to be sorted alphabetically just the same.

The initial string of ORDER BY business_name ASC worked well as it displayed all the business well. I did this to see if my search worked.

I tried doing ORDER BY business_name ASC, verification_status DESC");` and nothing displayed.

You could use CASE :

ORDER BY CASE verification_status 
            WHEN 'gold'   THEN 1 
            WHEN 'silver' THEN 2
            WHEN 'bronze' THEN 3 
            ELSE 4
         END,
         business_name ASC
... ORDER BY FIELD(verification_status, 'gold', 'silver', 'bronze')

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