Check if any Select output line in Mysql contains a value

Asked

Viewed 823 times

2

I would like to know how to check in the Where clause if any of the lines has a certain number. For example: If a select returns

1
2
3
4

and in clause ask the 4 it returns the values, if return

1
2
3

and in clause ask the 4 it returns empty.

I do not know if it was clear, if there is doubt I rewrite the question. Thank you from now.

1 answer

1


If I understand correctly, I think the use of the clause EXISTS does what you want.
To query would be something like that:

SELECT numero FROM numeros WHERE EXISTS(SELECT numero FROM numeros WHERE numero = 4)  

EXISTS returns true if the subquery return some line.

If number 4 exists in the table EXISTS returns true causing the query return the numbers.
Otherwise EXISTS returns false and the query return zero lines

See working on Sqlfiddle

  • That was it, and it was very simple. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.