表A
itemNo colorNo
1 3
1 4
2 4
2 70
3 9
3 10
我想这样做......
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';
但我可以将这3个查询合并为一个吗?
我尝试这样做,但它只返回满足最后条件的一行。
SELECT *
FROM A
WHERE ((itemNo = '1' AND colorNo = '4')
or (itemNo = '2' AND colorNo = '70')
or (itemNo = '3' AND colorNo = '9'));
编辑:事实证明我得到的桌子有问题。前两个'itemNo'甚至不存在!难怪只有最后一个被归还。感谢所有帮助过的人!我会把它留在这里,希望它能帮助有类似问题的人。
答案 0 :(得分:9)
尝试
SELECT *
FROM A
WHERE (itemNo = '1' AND colorNo = '4')
OR (itemNo = '2' AND colorNo = '70')
OR (itemNo = '3' AND colorNo = '9')
或者您也可以这样做
SELECT *
FROM A
WHERE (itemNo, colorNo) IN ((1, 4),(2, 70),(3, 9))
输出:
| ITEMNO | COLORNO | -------------------- | 1 | 4 | | 2 | 70 | | 3 | 9 |
这是 SQLFiddle 演示
答案 1 :(得分:0)
SELECT
*
FROM
A
WHERE
(itemNo = '1' AND colorNo = '4') OR
(itemNo = '2' AND colorNo = '70') OR
(itemNo = '3' AND colorNo = '9');
是你的意思吗?