MySQL:SELECT IF()语句中的SELECT

时间:2013-01-13 01:14:40

标签: mysql sql select

我可以拥有这样的东西吗?

SELECT IF((SELECT COUNT(id) FROM table WHERE id = 1) = 0, 1, <nothing_to_happen>) AS Available

我的目标是选择这个:

+---------+
|Available|
+---------+
|1        |
+---------+

仅当从此查询中未选择任何行时:

SELECT id FROM table WHERE id = 1

如果表中存在id = 1的行,我希望我的查询返回零行!这可能吗?

2 个答案:

答案 0 :(得分:5)

SELECT  1
FROM    dual
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    mytable
        WHERE   id = 1
        )

答案 1 :(得分:0)

查询:

SELECT CASE
           WHEN COUNT(id) = 0 THEN 1
           ELSE 0
       END AS Available
FROM `TABLE`
WHERE id = 1