3

I have a very simple question: how can I check multiple MySQL columns against a single value? I'm looking basically for the opposite of the IN (1, 2, 3, 4) where one can check a single column against multiple values.

I know I can just write multiple sentences, but I was looking for a more elegant solution. I'm using PHP. Thank you!

1
  • 1
    Why didn't you try the opposite. The opposite works. First try then ask the question Commented Jan 18, 2013 at 16:18

3 Answers 3

8

Use IN only like below:

select * from TableName where 2 IN (ColumnOne,ColumnTwo,ColumnThree)
Sign up to request clarification or add additional context in comments.

Comments

6

is this what you're looking for?

SELECT *
FROM   tableName
WHERE  1 IN (col1, col2, col3, col4)

5 Comments

Thanks, I also found out that this works: SELECT * FROM tableNAME WHERE (col1, col2) = value
Actually there is a problem with the solution you provided. The IN statement works as an OR operand. What I need is that the conditions are met for ALL the columns (AND operand).
then why not execute SELECT * FROM tableName WHERE col1 = 1 AND col2 = 1 AND col3 = 1 AND col4 = 1 ?
I know, I was just asking if there was a more elegant solution for the problem, avoiding to get the query long (I'm creating it in a PHP script and the criteria is a quite long class property). If there is no IN-like operand that uses an AND logic instead of an OR logic I guess I have no other way.
I need same...but just need like on value... WHERE 'value%' IN (col1, col2, col3, col4) I want to run a big query with large data..
0

How about using or logical operator on your where clause?

select *
  from your_table
 where col_a = 'value'
    or col_b = 'value'
    or col_c = 'value'
    or col_d = 'value'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.