Show records containing a specific value first in MySQL

I need entries in my database that will appear in a list. If “characteristic” is indicated in the list, the value in the column with the yes function.

I'm not sure which MySQL query would give me this result, or if it even exists. But another idea that I have is to have one query that gets all recognized and lists them, and then another gets all the lists that don't appear.

Do you have any ideas? Thanks at Advance!

+3
source share
2 answers

Use 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: SomeOtherColumnNameForAMinorKeySort, , .

+4
SELECT fields FROM table ORDER BY featured DESC;
0

Source: https://habr.com/ru/post/1717989/


All Articles