这是我的问题:
SELECT
CASE WHEN if_this_matches_nothing(SELECT 1 FROM mytable WHERE id = :id) THEN 0
ELSE 1 END
MySQL中是否有这样的if_this_matches_nothing
函数?
答案 0 :(得分:3)
您正在寻找NOT EXISTS
:
SELECT (CASE WHEN NOT EXISTS (SELECT 1 FROM mytable WHERE id = :id)
THEN 0 ELSE 1
END) as flag
你实际上可以缩短它。 MySQL中不需要CASE
:
SELECT (NOT EXISTS (SELECT 1 FROM mytable WHERE id = :id) ) as flag