Wednesday, 27 May 2015

Mysql: How to select rows with multiple specific column values in single query?

Table A
itemNo   colorNo
1        3
1        4
2        4
2        70
3        9
3        10
 
Try

SELECT *
  FROM A
 WHERE (itemNo = '1' AND colorNo =  '4')
    OR (itemNo = '2' AND colorNo = '70')
    OR (itemNo = '3' AND colorNo =  '9')

or you can also do this
SELECT * FROM A WHERE (itemNo, colorNo) IN ((1, 4),(2, 70),(3, 9)) Output:
| ITEMNO | COLORNO |
--------------------
| 1 | 4 |
| 2 | 70 |
| 3 | 9 |
 

0 comments:

Post a Comment