Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: SELECT inside SELECT IF() statement

Tags:

sql

select

mysql

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?

like image 545
matheuscscp Avatar asked Jun 16 '26 14:06

matheuscscp


1 Answers

SELECT  1
FROM    dual
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    mytable
        WHERE   id = 1
        )
like image 199
Quassnoi Avatar answered Jun 18 '26 03:06

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!