我的博客頁面中存在一個SELECT查詢問題。SELECT查詢IF條件
當評論狀態= 1時,我想評論每個博客的評論數。
我申請以下查詢..
SELECT CONCAT(u.first_name," ",u.last_name) name,r.*,IF(c.status=1,COUNT(c.id)) as comment
FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
,但它給SYNTEX錯誤..
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') as comment FROM users u RIGHT JOIN resources r ON u.id = r.created_by LEFT JOI' at line 1
請告訴我,我錯了。
由於
請出示你的語法錯誤,以便有人能幫助 – Pritamkumar
你的if語句僅提供真實的部分即IF(條件,TRUE,FALSE)。更改爲IF(c.status = 1,COUNT(c.id),0) – jeff
更新請檢查... – GYaN