首页 > 数据库 > MySQL > mysql 实现类似于oracle的merge语句
定期更新一个表A,但是表的数据来自于另一张表B。 A表和B表的结构一样, 当A表中的数据在B表中不存在时,把B表的数据添加到A表中; 当A表中的数据在B表中存在时(即唯一索引项相同时),把B表中的数据累加到A表中。 insert into tableA select from tableB on duplicate key update tableA.column= +tableB.column...; insert into tableA(key1,key2,col1,col2) select key1,key2,col1,col2 from tableB on duplicate key update col1=col1+tableB.col1,col2=col2+tableB.col2; insert into tableA(key1,key2,col1,col2) select key1,key2,col1,col2 from (here :it also can be a temp table)tableB on duplicate key update col1=col1+tableB.col1,col2=col2+tableB.col2;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24349972/viewspace-2168419/ ,如需转载,请注明出处,否则将追究法律责任。