/*Merge into 详细介绍
MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。
这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。
*/
/*语法:
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
*/
merge into emp_new t1
using (select career,max(age) as age
from emp
group by career) t2 //要与max()一起查询的列,必须用group by修饰
on (t1.career = t2.career and t1.age = t2.age)
when matched then
update set t1.age = t2.age
when not matched then
insert (career,age) values (t2.career,t2.age)