insert中的子查询

thtygnil  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(271)

我正在尝试在插入查询中使用子查询,但出现错误。我怎样才能解决这个问题?

insert into classes_has_students (id,student_id,class_id) 
values (
    (select id from students where first_name = 'Subhan'),
    (select id from classes where class_name = 'FSC')
)
vq8itlhq

vq8itlhq1#

你有一个额外的 id 在列列表中。大概,它是自动分配的,所以您可以忽略它:

insert into classes_has_students (student_id, class_id) 
    values ( (select id from students where first_name = 'Subhan'),
             (select id from classes where class_name = 'FSC')
           )

否则,您需要给它一个值:

insert into classes_has_students (id, student_id, class_id) 
    values ( 42,
             (select id from students where first_name = 'Subhan'),
             (select id from classes where class_name = 'FSC')
           )

相关问题