我想将数据插入到包含3个不同表的值
的表中 table class_teachers_section
class_ref teachers_ref section_ref
2 3 2
1 6 2
表类 id名称小时 1 pe 3
表教师 id first_name last_name 1 Lenovo Lenovo表格部分 id名称 1年级4个
class_teachers_section是一个中间表...这是我的陈述
INSERT INTO class_teachers_section(class_ref, teachers_ref, section_ref)
values (
(class_ref, (select class.id as class_ref from class where class.name = 'pe')),
(teachers_ref, (select teachers.id as teachers_ref from teachers where
teachers.last_name = 'lenevo')),
(section_ref, (select section.id as section_ref from section where section.name =
'grade 4'))
)
你们有任何想法如何解决这个问题吗?感谢
尝试了这个..
INSERT INTO class_teachers_section(class_ref, teachers_ref, section_ref)
values( (select class.id as class_ref from class where class.name = 'pe'),
(select teachers.id as teachers_ref from teachers where teachers.last_name = 'lenevo'),
(select section.id as section_ref from section where section.name = 'grade 4'))
teachers_ref不能为空错误
答案 0 :(得分:0)
你的语法很不寻常。 insert
列表中有三列,但values
列表中有六个元素。其中三个似乎是列名。
这不是select
的工作原理。相反,列对应的位置。
但是,在您的情况下,您应该使用insert . . . select
:
INSERT INTO class_teachers_section(class_ref, teachers_ref, section_ref)
select (select class.id as class_ref from class where class.name = 'pe'),
(select teachers.id as teachers_ref from teachers where teachers.last_name = 'lenevo'),
(select section.id as section_ref from section where section.name = 'grade 4');