在 Oracle 中,是否可以union在单列而不是整行上执行重复条件?
我有表A,B有 2 列:item_name, price. 我想创建一个视图item_names,它肯定会在表格A中查看是否item_name存在,如果存在,则使用pricein A,如果不去B并使用pricein B,那么union其余的item_nameinB尚未添加到视图。
例如,
Table A Table B
---------------- ----------------
item_name price item_name price
---------------- ----------------
shoe 10 shoe 8
socks 2 socks 4
shirt 5 t-shirt 3
gloves 1 glasses 15
pants 7
shoe如果有的话,我socks想使用table A's 价格,如果没有,我想使用table B. 所以最后,我的观点应该是这样的:
View
-----------------------
item_name price source
-----------------------
shoe 10 A
socks 2 A
t-shirt 3 B
glasses 15 B
pants 7 B
我试过了
select * from A a
where item_name in ('shoe', 'socks')
union
select * from B b
where b.item_name not in
(select item_name from A
where item_name in ('shoe', 'socks'))
我不喜欢,因为查询select * from A where item_name in ('shoe', 'socks')是重复的。有没有更好/更有效的方法来做到这一点?