MySql如果然后在Select语句中 [英] MySql If Then in Select Statement
本文介绍了MySql如果然后在Select语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在mysql select中使用IF THEN语句,但无法弄清楚.如果尚无注释,则commentcreated值应为项目本身的创建值.这是查询:
I want to use an IF THEN statement in a mysql select, but can't figure it out. When there are no comments yet, the commentcreated value should be the created value of the item itself. This is the query:
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'd figured this, but it doesn't work:
...
, IF max(comments.created)!='NULL'
THEN max(comments.created) AS commentcreated
ELSE item.created AS commentcreated
END IF;
FROM .......
(最佳)方法是什么?
推荐答案
可以使用IFNULL
IFNULL(max(comments.created), item.created) AS commentcreated
或IF
IF(max(comments.created)='NULL', max(comments.created), item.created)
AS commentcreated
这篇关于MySql如果然后在Select语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文