MySQL query to show records with current date on top and others as per descending order

佐手、 提交于 2019-12-04 10:24:49

Try this ORDER BY clause with condition -

ORDER BY IF(sale_ends = DATE(NOW()), 0, 1), sale_ends DESC

You can wrap the whole thing in another SELECT and use ORDER BY

SELECT * FROM (     
     SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends 
     FROM tbl_sales b WHERE b.active=1 
     UNION
     SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends 
     FROM tbl_sales b INNER JOIN tb_category c ON  b.category_id=c.cat_id 
     WHERE c.cat_keyword LIKE 'a' ORDER BY sale_ends  DESC
) AS all_sales
ORDER BY (sale_ends=CURDATE()) DESC, sale_ends DESC
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!