gpt4 book ai didi

mysql - 如何将一个 mysql 表中的 INSERT INTO 插入到另一个表中并设置一列的值?

转载 作者:可可西里 更新时间:2023-11-01 06:35:53 25 4
gpt4 key购买 nike

我需要将表 1 中的数据插入到表 2 中。但是,我想将 table2 中的 myYear 列设置为 2010。但是,table1 中没有 myYear 列。

所以,我的基本插入内容如下:

INSERT INTO  `table2` ( place, event ) 
SELECT place, event
FROM table1

大致上,我想做如下事情:

INSERT INTO `table2` ( place, event, SET myYear='2010' )
...

有没有办法在插入语句中设置列值?

最佳答案

应该做到以下几点:

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)

关于mysql - 如何将一个 mysql 表中的 INSERT INTO 插入到另一个表中并设置一列的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com