1

Table A

itemNo   colorNo
1        3
1        4
2        4
2        70
3        9
3        10

I wanted to do this...

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

But can I combine those 3 queries into one?

I tried to do this, but it only returned one row satisfying the last condition.

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

EDIT: It turns out the table I got is faulty. The first two 'itemNo' didn't even exist! No wonder only the last one got returned. Thank you to everyone who helped! I'll leave this up here and hopefully it'll help someone with a similar question.

5
  • 2
    Your second query is perfect. It returns three rows. What's the problem now? Commented Jul 26, 2013 at 9:43
  • You can also use UNION, or OR conditions Commented Jul 26, 2013 at 9:44
  • Strange. I'm using HeidiSQL and it's giving me just one result. Maybe there's some differences between HeidiSQL and normal MySQL? Commented Jul 26, 2013 at 9:47
  • HeidiSQL is just another MySQL client. There is no reason for it to return different results for the same query on the same server. Commented Jul 26, 2013 at 10:19
  • There seems to be no need for all those inverted commas. Commented Jul 26, 2013 at 12:13

2 Answers 2

8

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 |

Here is SQLFiddle demo

Sign up to request clarification or add additional context in comments.

2 Comments

This is the perfect query. But he has already tired the same. Don't know why he is not getting expected result.
0
SELECT 
    *
FROM 
    A
WHERE 
    (itemNo = '1' AND colorNo = '4') OR
    (itemNo = '2' AND colorNo = '70') OR
    (itemNo = '3' AND colorNo = '9');

is that what you mean?

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.