Mateusz Kapusta
Mateusz Kapusta

Reputation: 83

Checking multiple MySQL columns against a single value

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!

Upvotes: 3

Views: 1928

Answers (3)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16076

Use IN only like below:

select * from TableName where 2 IN (ColumnOne,ColumnTwo,ColumnThree)

Upvotes: 8

John Woo
John Woo

Reputation: 263693

is this what you're looking for?

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

Upvotes: 6

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

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'

Upvotes: 0

Related Questions