Reno Reno - 2 months ago 15
MySQL Question

MySQL Making an exception to an ORDER?

I have a query like this query below.

SELECT name, id, note_date, note
FROM example
ORDER BY name, note_date


Lets say I have a user named "John Smith". I want the query to work as is, except I want all entries by John Smith to violate the order and appear at the top of the list.

I was just wondering if there's a simpler or more proper way to do this other than a JOIN (like below).

Also, if it matters, "John Smith" may or may not have entries at any given time.

Thanks!

Like:

SELECT name, id, note_date, note
FROM example
WHERE name = "John Smith"
ORDER BY note_date
JOIN
SELECT name, id, note_date, note
FROM example
WHERE name != "John Smith"
ORDER BY name, note_date

Answer Source
 SELECT name, 
             id, 
             note_date,
             note, 
             CASE 
               WHEN name= 'John Smith' 
                  THEN 0 
                ELSE 1 
               END AS OrderStatement
 FROM example
 ORDER BY OrderStatement, name, note_date

Just you need to create one more column to generate custom order and use it in the ORDER BY clause along with the other columns.