php - Mixed sorting of dates in MySQL -


i have date column holds either date value or null. use hold product's expiration date. want write query fetch them in order: not yet expired, no expiration (null), , expired.

for example, assuming today may 15:

prd_id   prd_name   expiry date ----------------- 1    name1         may 16 2    name2         may 17 3    name3         may 18 4    name4         may 21 5    namex         null 6    namex         null 7    namex         null 8    namex         may 14 9    namex         may 12 

(a null value denotes no expiration)

how this?

you can try follwing syntax:-

select * your_table order case when expire_date > curdate() 1 end, when expire_date null 2 desc end, when expire_date < curdate() 3 desc end; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -