试图将一个表中的id作为外键插入第二个表时,mysql错误代码为1064
我正在尝试创建一个存储过程,以便在部门中添加讲师。 问题是,我无法根据输入的名字和姓氏从一个表中选择讲师id,并将其作为外键插入第二个表中 这是我收到的错误:试图将一个表中的id作为外键插入第二个表时,mysql错误代码为1064,mysql,sql,Mysql,Sql,我正在尝试创建一个存储过程,以便在部门中添加讲师。 问题是,我无法根据输入的名字和姓氏从一个表中选择讲师id,并将其作为外键插入第二个表中 这是我收到的错误: Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use nea
Error code 1064, SQL state 42000: You have an error in your SQL
syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'instructor.id WHERE
research_dep.name = researchName and instructor.firstname =
' at line 4
这就是我正在尝试的程序
DELIMITER @@
CREATE PROCEDURE sp_add_instructor_to_research
(iFirstName varchar(60)
, iLastName varchar(60)
, researchName varchar(60))
BEGIN
INSERT INTO research_dep.fk_instructor_id
SELECT id
FROM instructor
WHERE instructor.firstname = iFirstName
and instructor.lastname = iLastName
and research_dep.name = researchName;
END @@
DELIMITER ;
您需要在insert语句中单独指定列名,例如:
INSERT INTO TABLE (COLUMN1) SELECT COLUMN2 FROM TABLE2;
您的查询将如下所示:
INSERT INTO research_dep (instructor_id)
SELECT id
FROM instructor
WHERE instructor.firstname = iFirstName
and instructor.lastname = iLastName
and research_dep.name = researchName;
您没有提供正在操作的表的任何详细信息,这通常是个好主意。然而,在这种情况下,它并不真正相关
and research_dep.name = researchName;
不能从未将其用作数据源(在“发件人”列表中)的查询中引用要插入的表(research_dep)。即使该表在from列表中,也需要为其分配不同的别名并添加join子句
此外,您不能插入到字段中-您可以插入到表中(括号中有可选的字段列表)。e、 g
我无法从您提供的代码中看出您打算查询做什么,因此无法建议查询应该是什么。我在您的查询中没有看到
和讲师.firstname=
之后的research\u dep.name=researchName
。这是您使用的查询吗?问题中的查询与错误消息不匹配。在错误消息中,讲师.id
位于where
关键字前面。
INSERT INTO research_dep (fk_instructor_id)