我需要将table1中的数据插入table2。但是,我想将table2中的myYear列设置为2010.但是,table1中没有myYear列。
所以,我的基本插图看起来像:
INSERT INTO `table2` ( place, event )
SELECT place, event
FROM table1
粗略地说,我想做类似以下的事情:
INSERT INTO `table2` ( place, event, SET myYear='2010' )
...
有没有办法在insert语句中设置列值?
答案 0 :(得分:9)
以下应该这样做:
INSERT INTO `table2` (place, event, myYear)
SELECT place, event, '2010'
FROM table1;
基本测试用例:
CREATE TABLE t1 (a int, b int);
CREATE TABLE t2 (c int);
INSERT INTO t2 VALUES (1),(2),(3),(4),(5);
INSERT INTO t1 SELECT c, 100 FROM t2;
SELECT * FROM t1;
+------+------+
| a | b |
+------+------+
| 1 | 100 |
| 2 | 100 |
| 3 | 100 |
| 4 | 100 |
| 5 | 100 |
+------+------+
5 rows in set (0.00 sec)
答案 1 :(得分:3)
INSERT INTO `table2` (place, event, myYear)
SELECT place, event, 2010
FROM table1
编辑:呸,没有得到答案张贴通知:P