我想在mysql select中使用IF THEN语句,但无法弄明白.当还没有评论时,评论创建的值应该是项目本身的创建值.这是查询:
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
Run Code Online (Sandbox Code Playgroud)
我想到了这个,但它不起作用:
...
, IF max(comments.created)!='NULL'
THEN max(comments.created) AS commentcreated
ELSE item.created AS commentcreated
END IF;
FROM .......
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
ajr*_*eal 10
可以使用 IFNULL
IFNULL(max(comments.created), item.created) AS commentcreated
Run Code Online (Sandbox Code Playgroud)
要么 IF
IF(max(comments.created)='NULL', max(comments.created), item.created)
AS commentcreated
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13501 次 |
| 最近记录: |