我不確定案例如何與mysql一起工作,任何人都可以解釋這個查詢有什麼問題嗎?查詢不起作用時選擇案例
CASE WHEN SELECT COUNT(*) from websites where website_id = 171 and master = 2 > 0
SELECT timestamp from websites where website_id = 171 and master = 2
ELSE SELECT NOW() as timestamp
我不確定案例如何與mysql一起工作,任何人都可以解釋這個查詢有什麼問題嗎?查詢不起作用時選擇案例
CASE WHEN SELECT COUNT(*) from websites where website_id = 171 and master = 2 > 0
SELECT timestamp from websites where website_id = 171 and master = 2
ELSE SELECT NOW() as timestamp
退房MySQL Control Flow Functions
CASE ...當需要具有THEN
關鍵字工作。
你可能想:
CASE
WHEN (SELECT COUNT(*) from websites where website_id = 171 and master = 2) > 0
THEN (SELECT timestamp from websites where website_id = 171 and master = 2)
ELSE NOW()
END as timestamp
如果只有兩種可能性,不過,你如果會更好:
IF((SELECT count(*) from websites where website_id = 171 and master = 2) > 0,
(SELECT timestamp from websites where website_id = 171 and master = 2),
NOW()) AS TIMESTAMP
或者,你可以使用IFNULL並跳過COUNT(* )
IFNULL((SELECT timestamp from websites where website_id = 171 and master = 2),
NOW()) AS TIMESTAMP
當使用嵌入選擇獲取值時,您需要將select放在括號中。此外,CASE
需要關閉END
。
試試這個:
CASE
WHEN (SELECT COUNT(*) from websites where website_id = 171 and master = 2) > 0
THEN (SELECT timestamp from websites where website_id = 171 and master = 2)
ELSE (SELECT NOW())
END as timestamp
我在所有這3個查詢中遇到了錯誤:'' #1064 - 您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'IFNULL'附近使用正確的語法,在'IF'(SELECT和CASE WHEN)處使用相同的語句 - 不確定是什麼我在做錯了,我在phpmyadmin中運行這些文件 – HyderA
哇,我遺漏了幾個paren的文件 – cwallenpoole
這很奇怪,我仍然遇到同樣的錯誤 – HyderA