INSERT INTO SELECT
语法说明
INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。且目标表中任何已存在的行都不会受影响。
语法结构
1. INSERT INTO table2 (column1, column2, column3, ...)
2. SELECT column1, column2, column3, ...
3. FROM table1
4. WHERE condition;
示例
1. create table t1(id int, name varchar(10));
2. insert into t1 values(1, 'a');
3. insert into t1 values(2, 'b');
4. insert into t1 values(3, 'c');
5. create table t2(id int, appname varchar(10), country varchar(10));
6. insert into t2 values(1, 'appone', 'CN');
7. insert into t2 values(2, 'apptwo', 'CN');
8. INSERT INTO t1 (name) SELECT appname FROM t2;
10. mysql> select * from t1;
11. +------+--------+
12. | id | name |
13. +------+--------+
14. | 1 | a |
15. | 2 | b |
16. | 3 | c |
17. | NULL | appone |
18. | NULL | apptwo |
19. +------+--------+
