Santin
Santin

Reputation: 176

SQL return conditional

How do I do to select all or nothing using SQL, following the condition: If any tuple has a column with the value 1 it must return empty, however if all have the value 0 it return all tuples.

Upvotes: 2

Views: 104

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

You can do it like this:

SELECT *
FROM MyTable
WHERE (SELECT COUNT(*) FROM MyTable t WHERE t.MyColumn=1)=0

The condition evaluates to true or false for all rows; if any row has 1 in it, no rows would be returned.

Upvotes: 3

Related Questions