I want to use the IF THEN statement in mysql selection, but can't figure it out. When there are no comments yet, the value created by the comment must be the created value of the element itself. This is the request:
SELECT item.*,
count(comments.itemid) AS commentcount,
max(comments.created) AS commentcreated
FROM table_items AS item
LEFT JOIN table_comments AS comments ON (comments.itemid = item.id)
WHERE item.published=1
GROUP BY item.id
ORDER BY item.created DESC
I would think about this, but it does not work:
...
, IF max(comments.created)!='NULL'
THEN max(comments.created) AS commentcreated
ELSE item.created AS commentcreated
END IF;
FROM .......
What is the best way to do this?
source
share