Ezra Polson
Ezra Polson

Reputation: 235

sql in operator on multiple columns

I have a list of values , for example id= ('10','20','30') I want to check if these values exisit in either columnA, columnB or columnC

This is what I have and this works fine

  "select * from Table1 where ColumnA in", id

What I am trying to do is

  "select * from Table1 where (ColumnA or ColumnB or ColumnC) in", id

I am getting a bunch of errors, so need help with the syntax

Upvotes: 1

Views: 106

Answers (3)

Karthik Swaminathan
Karthik Swaminathan

Reputation: 147

From what i could deduce from your question there, you want to find which of the given values exist in the given columns. This should be easy to get as below:

SELECT * FROM table1 WHERE ColumnA IN(10,20,30) AND ColumnB IN (10,20,30) AND ColumnC IN (10,20,30);

Upvotes: 0

Bjoern
Bjoern

Reputation: 16304

As far as I know there is no way to shorten this any further, you have to stick to

SELECT * 
FROM `yourtable` 
WHERE `ColumnA` IN ('10','20','30')
    OR `ColumnB` IN ('10','20','30') 
    OR `ColumnC` IN ('10','20','30');

Upvotes: 0

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

Try this instead:

select * from Table1 where ColumnA in ('10','20','30') or
                           ColumnB in ('10','20','30') or
                           ColumnC in ('10','20','30')

Upvotes: 2

Related Questions