Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Case Syntax

Tags:

php

mysql

SELECT *
FROM case_study
ORDER BY CASE WHEN expiry_date_case > CURDATE() THEN 1 END
WHEN expiry_date_case IS NULL THEN 2 Desc END
WHEN expire_date_case < CURDATE() THEN 3 Desc END;

When I am trying to run this query i am getting error as

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN expiry_date_case IS NULL THEN 2 Desc END WHEN expire_date_case < CURDATE() ' at line 4


Where am i doing wrong?

like image 907
mask man Avatar asked Sep 27 '22 18:09

mask man


1 Answers

Try

SELECT *
FROM case_study
ORDER BY CASE WHEN expiry_date_case > CURDATE() THEN 1 
WHEN expiry_date_case IS NULL THEN 2 
WHEN expire_date_case < CURDATE() THEN 3 END Desc

You have too many End's and Too many desc's

like image 163
Fred Avatar answered Oct 07 '22 21:10

Fred