Skip to content
Advertisement

If all result equals, return true

I have a simple select that returns a single column:

result: {1,1,2}

I want to check if all result are 1 then return true and if one column have a 2 return false.

Tried something like these:

SELECT IF(items.status=1, 'true', 'false') AS result
FROM bd.items 
WHERE items.id=33

But obviously it returns true if a value is 1 and false if it is 2, instead of just one result.

For example:

{1,1,1}=true

{1,2,1}=false

{1}=true

{2}=false

Advertisement

Answer

Is this what you want?

select min(status = 1) as result
from bd.items
where id = 33

This returns 1 if and only if all rows have status 1. If there is any other (non-null) value in the column, then this returns 0.

If you want this for all items at once:

select id, min(status = 1) as result
from bd.items
group by id
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement