mysql - 多个插入和选择到单个查询中

标签 mysql sql database

有没有办法将这种多次插入和选择转换为仅一条语句?

INSERT INTO `subject` (`subject_code`, `student_id`)
   SELECT 'mathematics', student_id FROM student;
INSERT INTO `subject` (`subject_code`, `company_id`)
   SELECT 'science', student_id FROM student;
INSERT INTO `subject` (`subject_code`, `company_id`)
   SELECT 'chemistry', student_id FROM student;

最佳答案

使用union allunion(取决于你的数据)

INSERT INTO `subject` (`subject_code`, `student_id`)
(SELECT 'mathematics' as subjectcode, student_id FROM student 
union all
SELECT 'science', student_id FROM student 
union all
SELECT 'chemistry', student_id FROM student);

关于mysql - 多个插入和选择到单个查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925196/

相关文章:

mysql - 错误 : argument of AND must be type boolean, 未输入文本

php - 如何自动将当前日期时间添加到我的数据库中

python - 如何打开sqlite数据库并将其转换为pandas数据框

mysql - MySQL 中何时使用单引号、双引号和反引号

php - 如何使用 Php 将我的 Android 应用程序连接到 Amazon RDS MySQL 数据库?

java - 如何使用hibernate将文件存储在数据库中

mysql - 如何在 Sql 触发器中获取当前已删除行的值并使用已删除的行数据更新另一个表

mysql - 正则表达式不适用于单/双引号

database - Impala中的数据库架构

mysql - 连接一条记录中的特定行并从第一个表中检索所有数据