简体   繁体   English

如何在MySQL的两个相对表中插入许多行?

[英]How to insert many rows into two relative tables in mysql?

I want to insert many rows into two relative tables. 我想在两个相对表中插入许多行。 I used LAST_INSERTED_ID() to insert data into the second table, but this id does not change 我使用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

Give this a try, where you declare a INT and then set that value, see below. 尝试一下,在此声明一个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;

You can also try this. 您也可以尝试一下。

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;

Make the inserts into assigned_students reference the just-inserted rows from themes (so you can get their auto generated IDs) also: 还可以使插入的assigned_students引用themes刚插入的行(这样您就可以获得其自动生成的ID):

BEGIN; 
  insert into themes (tutorID, year, theme_name, degreeID) 
    select themeID, year, work_name, degreeID from journal; 
  INSERT INTO assigned_students (studentID, tutorID, themeID, writing_language_id, work_typeID) 
    select j.studentID, j.tutorID, t.THEME_ID_COLUMN_NAME, 0, 4 
    from 
      journal j 
      inner join themes t on j.themeID = t.tutorid; 
COMMIT

Here we see that first we make some inserts into themes, and I presume it will autogenerate some IDs for themes' pk column. 在这里,我们看到首先我们在主题中插入了一些内容,我想它会为主题的pk列自动生成一些ID。

So we then join journal onto those rows we inserted so we can retrieve the generated IDs 因此,我们然后将日记加入到我们插入的那些行中,以便我们可以检索生成的ID

I don't know what the PK column of themes is called, so you'll have to replace THEME_ID_COLUMN_NAME with the correct value 我不知道themes的PK列叫什么,因此您必须将THEME_ID_COLUMN_NAME替换为正确的值

Note that you might have to specify additional columns in on j.themeID = t.tutorid than just the tutorid, if that doesnt uniquely identify a row 请注意,您可能必须on j.themeID = t.tutorid指定其他列,而不仅仅是tutorid,如果这不能唯一地标识一行

I read also that for autoincrement columns it is guaranteed that the IDs are sequential, so you can get the LAST_INSERTED_ID() which is the mos trecently inserted row) as well as the ROW_COUNT and hence know the range of IDs that was inserted, and use that to select/join the journal data 还读到 ,对于自动增量列,可以确保ID是连续的,因此您可以获得LAST_INSERTED_ID()(即mos中间插入的行)以及ROW_COUNT,因此知道了插入ID的范围,并可以使用选择/加入日记数据的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM