如何在 sql 查询中替换 UNION
How to replace UNION in sql query
我有一个 table: 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;
下面是return
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 运算符?或者有更好的实现方式吗?
只需将两个 where
子句放入一个查询中即可:
select id,name, code, attribute, amount
from attb
where (code = 123 and attribute='Alpha')
or 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
很容易。只需使用 or
.
select id,name, code, attribute, amount
from attb
where (code = 123 and attribute='Alpha') OR code = 122
试试这个。
select id,name, code, attribute, amount
from attb
where ((code = 123 and attribute='Alpha') or (code = 122))
select id,name, code, attribute, amount
from attb
where (code IN(122, 123) and attribute='Alpha') ;
我有一个 table: 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;
下面是return
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 运算符?或者有更好的实现方式吗?
只需将两个 where
子句放入一个查询中即可:
select id,name, code, attribute, amount
from attb
where (code = 123 and attribute='Alpha')
or 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
很容易。只需使用 or
.
select id,name, code, attribute, amount
from attb
where (code = 123 and attribute='Alpha') OR code = 122
试试这个。
select id,name, code, attribute, amount
from attb
where ((code = 123 and attribute='Alpha') or (code = 122))
select id,name, code, attribute, amount
from attb
where (code IN(122, 123) and attribute='Alpha') ;