sql - 如何使用 Select 语句将值插入到 MYSQL 表中

标签 sql mysql database insert

好吧,这个和我上一个很相似,但我不明白......!

我正在尝试以下操作:

Insert into table b
  (Select column_1 from table_a where ID = 1),
  (Select column_2 from table_a where ID = 1),
  0,
  (Select column_3 from table_a where ID = 1);

但我总是遇到语法错误...! 我认为我正在尝试做的事情是合乎逻辑的。

来自德国的问候,感谢您的回答!

最佳答案

非常接近 - 使用:

INSERT INTO TABLE_B
SELECT column_1, column_2, column_3 
  FROM TABLE_A
 WHERE id = 1

..假设 TABLE_B 中只有三列。否则,指定要插入的列:

INSERT INTO TABLE_B
  (column_1, column_2, column_3)
SELECT column_1, column_2, column_3 
  FROM TABLE_A
 WHERE id = 1

而且,如果需要——您也可以使用静态定义的值:

INSERT INTO TABLE_B
  (column_1, column_2, column_3, column_4)
SELECT column_1, column_2, 0, column_3 
  FROM TABLE_A
 WHERE id = 1

关于sql - 如何使用 Select 语句将值插入到 MYSQL 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908872/

相关文章:

php - 菜鸟MySql关于分页的问题;一次查询就够了吗?

mysql - 为什么 Laravel 5.8 不改表名迁移就不行?

MySQL程序同步2个表的模式

c# - 数据库未更新

mysql - 提高 JOIN 查询速度

sql - 在 bash 中从文件执行 SQL

database - 使用来自 mysql 转储的授权填充 mysql 数据库

MySQL 性能

php - 在 PHP 中使用电子邮件和密码注册后无法登录

php - Laravel PHP 和 Mysql : Convert table data into columns and rows by counting the instances of a string