我想在mysql select中使用IF THEN語句,但無法弄清楚。當沒有評論時,評論創建的值應該是該項目本身的創建值。這是查詢:MySql如果然後在選擇語句
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
我已經想通這一點,但它不工作:
...
, IF max(comments.created)!='NULL'
THEN max(comments.created) AS commentcreated
ELSE item.created AS commentcreated
END IF;
FROM .......
什麼(最好)的方式來做到這一點?
哇,謝謝!奇蹟般有效! – Bert 2010-12-17 22:20:59