简体   繁体   English

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

[英]How can I check sub query returns nothing?

Here is my query: 这是我的查询:

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

Is there such if_this_matches_nothing function in MySQL ? MySQL中有这样的if_this_matches_nothing函数吗?

You are looking for NOT EXISTS : 您正在寻找NOT EXISTS

SELECT (CASE WHEN NOT EXISTS (SELECT 1 FROM mytable WHERE id = :id)
             THEN 0 ELSE 1
        END) as flag

You can actually shorten this. 你实际上可以缩短它。 The CASE is not needed in MySQL: MySQL中不需要CASE

SELECT (NOT EXISTS (SELECT 1 FROM mytable WHERE id = :id) ) as flag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM