I have a table that has columns like this for example:
id,col1,col2,col3,col4
Now, I want to check if ANY of col1, col2, col3, col4 have the passed in value.
The long way to do it would be..
SELECT * FROM table WHERE (col1 = 123 OR col2 = 123 OR col3 = 123 OR col4 = 123);
I guess it’s the opposite version of IN.
Is there an easier way to do what I want?
You can use the
INpredicate, like so:SQL Fiddle Demo
No it is not, It is the same as using the
ORs the way you did in your question.To clarify this:
The predicate
INor set membership is defined as1:Where the
Value Expressioncan be either 2:So it is fine to do it this way, using the value expression
123, which is a literal.1, 2: Images from: SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL