mysql insert into select 语句的用法,从表2中复制表1中没有的数据到表1
微wx笑 2022-03-27【数据库】 4 0关键字: mysql sql insert into select
SQL INSERT INTO SELECT 语法
我们可以从一个表中复制所有的列插入到另一个已存在的表中:
INSERT INTO table2
SELECT * FROM table1;
SQL INSERT INTO SELECT 语法
我们可以从一个表中复制所有的列插入到另一个已存在的表中:
INSERT INTO table2 SELECT * FROM table1;
或者我们可以只复制希望的列插入到另一个已存在的表中:
INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;
实例
INSERT INTO Websites (name, country) SELECT app_name, country FROM apps WHERE id=1;
现实中用到的sql语句:
INSERT INTO stock ( `code`, `cnname`, `stname`, `zhangfu`, `price`, `zhangdie`, `buy`, `sale`, `zongliang`, `xianliang`, `zhangsu`, `huanshou`, `open`, `high`, `low`, `yestoday`, `peg`, `zongjine`, `liangbi`, `hangye`, `diqu`, `zhenfu`, `junjia`, `neipan`, `weipan`, `neiwaibi`, `buyliang`, `saleliang`, `wppl`, `kpje`, `kphsz`, `fengdane`, `fengdanbi`, `liutonggu`, `liutongsz`, `zongshizhi`, `qiangruodu`, `huoyuedu`, `junliang`, `bihuanshou`, `lianzhang`, `zhangfu3`, `huanshouz`, `liutongszz`, `beta`, `pettm`, `pes`, `zhangfukp`, `zhangfuzg`, `zhangfuzd`, `zhangfupj`, `zhangfust`, `huituobo`, `gongjibo`, `xianjuncha`, `updatecw`, `datess`, `zongguben`, `bagu`, `hgu`, `zongzichan`, `jingzichan`, `shaoshugq`, `zcfzl`, `zichanld`, `zichangd`, `zichanwx`, `ldfz`, `cunhuo`, `yszk`, `zbgjj`, `yingyesr`, `yingyecb`, `yingyelr`, `tuozisy`, `lirunze`, `lirunsh`, `jinglirun`, `lirunwfp`, `xianjljy`, `xianjlz`, `gudongrs`, `renjuncg`, `renjunsz`, `tongbilr`, `tongbisr`, `pe`, `shixianlu`, `shixiaolu`, `guxilu`, `meigusy`, `meigujz`, `tzhjz`, `meigugj`, `meiguwfp`, `quanyibi`, `roe`, `maolilu`, `yylirunlu`, `jlirunlu`, `flag` ) SELECT s2.`code`, s2.`cnname`, s2.`stname`, s2.`zhangfu`, s2.`price`, s2.`zhangdie`, s2.`buy`, s2.`sale`, s2.`zongliang`, s2.`xianliang`, s2.`zhangsu`, s2.`huanshou`, s2.`open`, s2.`high`, s2.`low`, s2.`yestoday`, s2.`peg`, s2.`zongjine`, s2.`liangbi`, s2.`hangye`, s2.`diqu`, s2.`zhenfu`, s2.`junjia`, s2.`neipan`, s2.`weipan`, s2.`neiwaibi`, s2.`buyliang`, s2.`saleliang`, s2.`wppl`, s2.`kpje`, s2.`kphsz`, s2.`fengdane`, s2.`fengdanbi`, s2.`liutonggu`, s2.`liutongsz`, s2.`zongshizhi`, s2.`qiangruodu`, s2.`huoyuedu`, s2.`junliang`, s2.`bihuanshou`, s2.`lianzhang`, s2.`zhangfu3`, s2.`huanshouz`, s2.`liutongszz`, s2.`beta`, s2.`pettm`, s2.`pes`, s2.`zhangfukp`, s2.`zhangfuzg`, s2.`zhangfuzd`, s2.`zhangfupj`, s2.`zhangfust`, s2.`huituobo`, s2.`gongjibo`, s2.`xianjuncha`, s2.`updatecw`, s2.`datess`, s2.`zongguben`, s2.`bagu`, s2.`hgu`, s2.`zongzichan`, s2.`jingzichan`, s2.`shaoshugq`, s2.`zcfzl`, s2.`zichanld`, s2.`zichangd`, s2.`zichanwx`, s2.`ldfz`, s2.`cunhuo`, s2.`yszk`, s2.`zbgjj`, s2.`yingyesr`, s2.`yingyecb`, s2.`yingyelr`, s2.`tuozisy`, s2.`lirunze`, s2.`lirunsh`, s2.`jinglirun`, s2.`lirunwfp`, s2.`xianjljy`, s2.`xianjlz`, s2.`gudongrs`, s2.`renjuncg`, s2.`renjunsz`, s2.`tongbilr`, s2.`tongbisr`, s2.`pe`, s2.`shixianlu`, s2.`shixiaolu`, s2.`guxilu`, s2.`meigusy`, s2.`meigujz`, s2.`tzhjz`, s2.`meigugj`, s2.`meiguwfp`, s2.`quanyibi`, s2.`roe`, s2.`maolilu`, s2.`yylirunlu`, s2.`jlirunlu`, s2.`flag` FROM `stock_2` as s2 LEFT JOIN stock as s1 on s2.code = s1.code WHERE s1.code IS null;
注意事项
1、要插入数据的表必须存在,但结构不需要和源数据表一致。
2、如果要插入数据的表里面已经存在数据,存在自增ID字段,那么在编写语句的时候要去掉ID字段。
3、如果希望将查询数据插入到一个不存在的表,需要使用 Select into 语句。
例如:
SELECT * INTO WebsitesBackup2016 FROM Websites WHERE country='CN';
复制多个表中的数据插入到新表中:
SELECT Websites.name, access_log.count, access_log.date INTO WebsitesBackup2016 FROM Websites LEFT JOIN access_log ON Websites.id=access_log.site_id;
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/database/2022-03-27/1138.html