基于当前表中的相同数据向表中插入数据

0x6upsns  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(253)

我有一张table,结构如下:

create table student_course(
student_id integer,
course_id integer,
primary key (student_id, course_id)
);

这里是我的用例:选择某些课程的学生需要参加另一个辅助课程。例如,一个注册了 course_id101 必须修一门额外的课程 course_id300 ,因为101太难了,他们应该同时上300的辅导课。
我需要创建一个sql查询,为选修本课程的学生添加额外的条目 student_course 表格;在本例中,为101到300的学生添加新行。
我现在正在使用 select * from students_course where course_id = 101 ,然后导出此数据以更新101到300的注册学生。最后,我用101再次导入这些输出的学生。有没有更好的sql查询可以用来插入当前表中的数据?

gk7wooem

gk7wooem1#

您基本上需要将insert查询与select一起使用。请尝试以下查询:

insert into student_course(student_id,courseId)
    select student_id, 300
    from student_course
    where course_id = 101;

注意:请确保您没有课程id为101且课程id为300的学生,否则此查询将不起作用。

相关问题