I am a total newb and never used case before, so please don’t laugh.
Why doesn’t this query work?
SELECT *
FROM `cronjob_reloaded`
WHERE `carid` LIKE '%bmw%'
OR `age` BETWEEN '10' AND '15'
ORDER BY
CASE WHEN `carid` LIKE '%bmw%' = 1
THEN 1
CASE WHEN `age` BETWEEN '10' AND '15' = 2
THEN 2
I get this error:
Check the manual that corresponds to your MySQL server version for the right syntax to use near ‘CASE WHEN
ageBETWEEN ’10’ AND ’15’ =2 THEN 2
Your syntax is a little strange. You don’t need the
= 1in theLIKEcase, the second condition has an invalid= 2, and since both are conditions of the same statement, don’t repeatCASE— instead begin the second condition withWHEN.It is also recommended to put in an
ELSEcase to match all other rows deterministically. Below, I insertedELSE 3, which sorts all other non-matched rows after the two matching conditions.Finally, the whole construct should end with an
ENDkeyword.