Chris Bier
Chris Bier

Reputation: 14455

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!

Upvotes: 3

Views: 1966

Answers (2)

David Andres
David Andres

Reputation: 31811

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.

Upvotes: 4

strager
strager

Reputation: 90062

SELECT fields FROM table ORDER BY featured DESC;

Upvotes: 0

Related Questions