如何检查子查询什么都不返回?

时间:2017-07-19 19:52:23

标签: mysql sql

这是我的问题:

SELECT
    CASE WHEN if_this_matches_nothing(SELECT 1 FROM mytable WHERE id = :id) THEN 0
    ELSE 1 END

MySQL中是否有这样的if_this_matches_nothing函数?

1 个答案:

答案 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