简体   繁体   中英

Show records that contain a certain value first in MySQL

I need the records in my database that are "featured" to be listed first. If a listing is "featured" the value in the featured column is "yes".

I am not sure what kind of MySQL Query would give me this result, or if it even exists. But the other idea I have is to have one query that gets all featured ones and lists them, and then another one gets all of the listings that aren't featured.

Do you have any ideas? Thanks in Advance!

Use an ORDER BY with a CASE statement, as in

SELECT * 
FROM TheTable
ORDER BY CASE LOWER(Featured)
           WHEN 'yes' THEN 0 
           ELSE 1 
         END 
         ASC,
         SomeOtherColumnNameForAMinorKeySort ASC

EDIT : Renamed RecordName to SomeOtherColumnNameForAMinorKeySort to better express what the column's purpose is.

SELECT fields FROM table ORDER BY featured 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