我有一张桌子:attb
------------------------------------------------
Id | Name | Code | Attribute | Amount
------------------------------------------------
1 | AV | 123 | Alpha | 233
------------------------------------------------
2 | TV | 122 | Beta | 235
------------------------------------------------
3 | TV | 123 | Gama | 238
------------------------------------------------
4 | CD | 122 | Beta | 239
------------------------------------------------
5 | TP | 122 | Beta | 240
------------------------------------------------
6 | RX | 123 | Alpha | 241
------------------------------------------------
我以以下方式查询它:
select id,name, code, attribute, amount
from attb
where code = 123 and attribute='Alpha'
UNION
select id,name, code, attribute, amount
from attb
where code = 122;
它返回以下内容
Id | Name | Code | Attribute | Amount
------------------------------------------------
1 | AV | 123 | Alpha | 233
------------------------------------------------
2 | TV | 122 | Beta | 235
------------------------------------------------
4 | CD | 122 | Beta | 239
------------------------------------------------
5 | TP | 122 | Beta | 240
------------------------------------------------
6 | RX | 123 | Alpha | 241
------------------------------------------------
有没有一种方法可以合并两个查询而不是使用UNION运算符?或任何更好的实现?
答案 0 :(得分:1)
只需将两个%
子句放在一个查询中:
where
输出:
select id,name, code, attribute, amount
from attb
where (code = 123 and attribute='Alpha')
or code = 122;
答案 1 :(得分:0)
非常容易。只需使用modules/
。
or
答案 2 :(得分:0)
尝试一下。
select id,name, code, attribute, amount
from attb
where ((code = 123 and attribute='Alpha') or (code = 122))
答案 3 :(得分:0)
select id,name, code, attribute, amount
from attb
where (code IN(122, 123) and attribute='Alpha') ;