[英]mysql insert with multiple select statement
I want to insert data to a table with values coming from 3 different tables 我想将数据插入到具有3个不同表的值的表中
table class_teachers_section 表class_teachers_section
class_ref teachers_ref section_ref class_ref Teachers_ref section_ref
2 3 2 1 6 2 2 3 2 1 6 2
table class id name hours 1 pe 3 表类ID名称小时1 pe 3
table teachers id first_name last_name 1 Lenovo Lenovo 表教师ID first_name last_name 1联想联想
table section id name 1 grade 4 表格部分ID名称1等级4
class_teachers_section is a intermediary table ... here is my statement 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'))
)
do you guys have any idea how to resolve this? 你们有什么想法要解决吗? thanks 谢谢
tried this .. 尝试了这个..
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 can not be null error teacher_ref不能为null错误
Your syntax is rather unusual. 您的语法很不寻常。 You have three columns in the insert
list but have six elements in the values
list. 您在insert
列表中有三列,但在values
列表中有六个元素。 Three of them appear to be column names. 其中三个似乎是列名。
That is not how select
works. 那不是select
工作原理。 Instead, the columns correspond by position. 而是,这些列按位置对应。
In your case, though, you should use insert . . . select
不过,就您而言,您应该使用insert . . . select
insert . . . select
insert . . . 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.