【发布时间】:2018-05-25 09:48:40
【问题描述】:
我需要对几个属性一一进行匹配。我希望避免使用多个选择语句。下面是例子。
Table1
Col1|Price|Brand|size
-----------------------
A|10$|BRAND1|SIZE1
B|10$|BRAND1|SIZE1
C|30$|BRAND2|SIZE2
D|40$|BRAND2|SIZE4
Table2
Col1|Col2|Col3
--------------
B|XYZ|PQR
C|ZZZ|YYY
Table3
Col1|COL2|COL3|LIKECOL1|Price|brand|size
-----------------------------------------
B|XYZ|PQR|A|10$|BRAND1|SIZE1
C|ZZZ|YYY|D|NULL|BRAND2|NULL
在table3中,我需要通过检查以下条件从table2中插入数据。
- 为表 2 中的记录查找匹配项,如果品牌和尺寸、价格匹配
- 如果未找到匹配项,则仅尝试品牌、尺寸
- 仍然找不到匹配项,仅尝试品牌
在上面的示例中,对于 table2 中的第一条记录,发现与所有 3 个属性匹配,因此插入到 table3 和第二条记录中,记录 'D' 匹配但只有 'Brand'。
我能想到的就是将 3 个不同的插入语句(如下所示)写入 oracle pl/sql 块。
insert into table3
select from tab2
where all 3 attributes are matching;
insert into table3
select from tab2
where brand and price are matching
and not exists in table3 (not exists is to avoid
inserting the same record which was already
inserted with all 3 attributes matched);
insert into table3
select from tab2
where Brand is matching and not exists in table3;
任何人都可以建议一种更好的方法来以更好的方式实现它,避免多次从 table2 中选择。
【问题讨论】:
标签: oracle plsql query-performance