I want to make an order by date to get the comments of user, but I want to give the importance to today date
if there's a comment today show it first and make the ordering with date for the rest.
I try to make this but always give me an error in syntax
SELECT *
FROM comment
ORDER BY IF(`DATE_TIME_COMMENT` = CURRENT_DATE())
is there any solution ?
First sort by a CASE
returning "something lower", if date_time_comment
is equal to current_date()
and "something higher" else. Then, second, sort by date_time_comment
.
SELECT *
FROM comment
ORDER BY CASE date_time_comment
WHEN current_date()
THEN 0
ELSE
1
END,
date_time_comment;
(Possibly add DESC
after date_time_comment
in the ORDER BY
clause, if you want to have newest comments first.)
If you can't have comments with future date, just order by date
SELECT * FROM comment ORDER BY DATE_TIME_COMMENT DESC
If you can have future date, and want to order first the today date, then comments with other date than today, one way to do this is with a UNION
SELECT * FROM (
SELECT 1 as order, c.* FROM comment c WHERE DATE_TIME_COMMENT = CURRENT_DATE()
UNION ALL
SELECT 2 as order, c.* FROM comment c WHERE DATE_TIME_COMMENT <> CURRENT_DATE()
) order by order asc, DATE_TIME_COMMENT desc
That you get first the today comments, then other comments order by date