我想在两个相对表中插入许多行。我使用了LAST_INSERTED_ID()将数据插入到第二个表中,但是这个ID没有改变
BEGIN;
insert into themes (tutorID, year, theme_name, degreeID)
select tutorID, year, work_name, degreeID from journal;
INSERT INTO assigned_students (studentID, tutorID, themeID, writing_language_id, work_typeID)
select studentID, tutorID, LAST_INSERT_ID(), 0, 4 from journal;
COMMIT
最佳答案
尝试一下,在这里声明一个INT
并设置该值,请参见下文。
BEGIN;
declare lastid INT;
insert into table1 (tutorID, year, name, degreeID)
select sm.tutorID, year, namework, dt.degreeID from table3;
set lastid = (SELECT LAST_INSERT_ID());
INSERT INTO table2 (studentID, tutorID, table1ID, writing_language_id, work_typeID)
select distinct StudentID, tutorID, lastid , 0, 4 from table3;
COMMIT;
你也可以试试这个。
BEGIN;
insert into table1 (tutorID, year, name, degreeID)
select sm.tutorID, year, namework, dt.degreeID from table3;
INSERT INTO table2 (studentID, tutorID, table1ID, writing_language_id, work_typeID)
select distinct StudentID, tutorID, (SELECT LAST_INSERT_ID()), 0, 4 from table3;
COMMIT;
关于mysql - 如何在MySQL的两个相对表中插入许多行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54308369/