
MERGE INTO table1 a
USING(
select b.id bid,b.name bname from table2 b left join table1 c on c.id = b.id
)bb on(a.id = bb.bid)
WHEN MATCHED THEN
UPDATE SET
a.name = bb.bname
WHEN NOT MATCHED THEN
INSERT(
a.id,a.name
)VALUES(
bb.bid,bb.bname
)
2.START WITH... CONNECT BY PRIOR...的作用,简单来说,就是将一个树状结构存储在一张表里,比如菜单表
select * from table_menu start with menu_id='210' connect by prior menu_id=menu_parent_id
查询菜单id为210下所有子菜单包括id为210的菜单。
3.OVER(PARTITION BY)根据表中字段分割后在排序取第一条
select * from (select r.*, row_number() over(PARTITION BY 字段1,字段2 order by 字段3 desc) rn
from table1 r
) where rn = 1
4.MINUS 表之间比较
select id,name from table1
minus
select id,name from table2
table1中有table2中无或者table1中id,name与table2中不同
Merge语句可以用来插入或更新数据,取决于数据是否已经存在。
语法结构:
例子:
新建两个表:customer,customer_import
执行语句:
执行后的customer表:
Mergeinto比Union要快得多,因为它只需要一次数据库访问就可以完成合并工作,而Union则需要两次访问才能实现合并。此外,Mergeinto还可以根据表中的索引来优化性能,而Union则不能。欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)