3

Can I have something like this?

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

My goal is select this:

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

Only if there is no row selected from this query:

SELECT id FROM table WHERE id = 1

If the row with id = 1 exists in table, I want my query to return zero rows! Is that possible?

2 Answers 2

5
SELECT  1
FROM    dual
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    mytable
        WHERE   id = 1
        )
Sign up to request clarification or add additional context in comments.

3 Comments

Using LEFT JOIN and filtering for NULL-s is another, probably faster solution.
It was marginally faster in 5.1, and become the same in 5.5.
Since when does MySQL have the dual table?
0

Query:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.