如何从一个mysql表插入到另一个表中,并设置一列的值?
我需要将表1中的数据插入表2中。但是,我想将表2中的myYear列设置为2010年。但是,表1中没有myYear列 因此,我的基本插入看起来像:如何从一个mysql表插入到另一个表中,并设置一列的值?,mysql,insert,Mysql,Insert,我需要将表1中的数据插入表2中。但是,我想将表2中的myYear列设置为2010年。但是,表1中没有myYear列 因此,我的基本插入看起来像: INSERT INTO `table2` ( place, event ) SELECT place, event FROM table1 大致上,我想做如下工作: INSERT INTO `table2` ( place, event, SET myYear='2010' ) ... 有没有办法在insert语句中设置列值?以下操作可以完成此
INSERT INTO `table2` ( place, event )
SELECT place, event
FROM table1
大致上,我想做如下工作:
INSERT INTO `table2` ( place, event, SET myYear='2010' )
...
有没有办法在insert语句中设置列值?以下操作可以完成此操作:
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)
编辑:呸,没有收到已发布的答案通知:p+1,用于显示已尝试的答案,以便我们可以准确地看到您正在尝试的内容。嗨,Dusty,谢谢您的正确答案。丹尼尔领先你3分钟;)谢谢你的帮助-拉西米地
INSERT INTO `table2` (place, event, myYear)
SELECT place, event, 2010
FROM table1