Check if any line of the Select result in Mysql contains a value

2

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

1
2
3
4

and in the request clause 4 it returns the values, if it returns

1
2
3

and in request clause 4 it returns empty.

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

    
asked by anonymous 16.04.2015 / 23:49

1 answer

1

If I understand correctly, I think that using the EXISTS clause does what you want. The query would be something like this:

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

EXISTS returns true if the subquery returns some line.

If there is a number 4 in the EXISTS table, it returns true causing the query to return the numbers. Otherwise EXISTS returns false and query will return zero rows

See working in SQLFiddle

    
17.04.2015 / 00:00