alwaysboots
alwaysboots

Reputation: 141

MySQL select based on the values of multiple columns

I want to be able to select rows based on the values of multiple columns, for example, given the following table:

column_1 | column_2 | column_3
------------------------------
1        | a        | b
2        | a        | c
3        | b        | z

I have no idea how to do so, and my current MySQL statement looks something like this:

SELECT * FROM table WHERE column_2, column_3 IN ((a, b), (b, z));

This doesn't work, but I hope what I am trying to achieve is apparent. Does anyone know how to do this?

Upvotes: 0

Views: 2118

Answers (2)

MrSimpleMind
MrSimpleMind

Reputation: 8647

As Barmar already mentioned the parentheses.. Or why not use logical query ;) Which some finds it more easier...

SELECT * FROM Table1 
WHERE
(column_2 = 'a' and column_3 = 'b')
or
(column_2 = 'b' and column_3 = 'z')
;

Upvotes: 0

Barmar
Barmar

Reputation: 782693

You need to put parentheses around the column names. And you need to quote strings.

SELECT * FROM table 
WHERE (column_2, column_3) IN (('a', 'b'), ('b', 'z'));

DEMO

Upvotes: 4

Related Questions