mysql从一个表创建多个表

时间:2013-12-16 23:56:10

标签: mysql sql

我有一个表已导入mysql。

现在我需要创建多个相关表。

所以基本上我现在有以下

start transaction;

Insert into Address (AddressLine1, AddressLine2, Town, County, Postcode)
Select Distinct Address_line_1, Address_Line_2, Town, County, Postcode from Import;

set addressLastId = last_insert_id();

INSERT INTO Organisation (Name, AddressID) 
SELECT DISTINCT Supplier_Name, addressLastId FROM Import;

commit;

我使用last_insert_id的第二部分从不增加,可能是因为它获得了最后一次插入。

所以我需要研究如何获取插入地址表的每一行的前一个id?

我需要在地址插入中有一个内部循环吗?

干杯

2 个答案:

答案 0 :(得分:1)

我同意蒂姆的观点。 在填充地址后,您可以运行

INSERT INTO Organisation (Name, AddressID)
SELECT DISTINCT Import.Supplier_Name, Address.id
FROM Import INNER JOIN Address ON (set all the address lines and city etc =, since Im guessing there wasnt an address ID in the original import)

答案 1 :(得分:0)

您可以使用连接进行第二次插入 - 连接地址和导入,并将每个字段中的必填字段插入到组织中。

获取最后一个插入ID只有在按顺序处理每个记录时才有效。

相关问题