0

I have one table which I have imported into mysql.

Now I need to create multiple related tables.

So basically I have currently got the following

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;

The second part where I use the last_insert_id never increments probably because it gets the last insert.

So I need to workout how i can get the previous id for each row inserted into the address table?

Would i need to have an inner loop within the address insert ?

Cheers

2 Answers 2

1

I agree with Tim. After you've populated Address, then you could just run

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)
Sign up to request clarification or add additional context in comments.

Comments

0

You could use a join for the second insert - join Address and Import, and insert the required fields from each into Organisation.

Getting the last insert ID will only work if you process each record sequentially.

6 Comments

Cheers. So how will i match the the correct address to the organisation as there is no unique id to determine this?
I would join on the 4 fields you have imported into address (so Address_line_1, Address_Line_2, Town, County) and return the ID from Address also. You probably need to use a left join and select distinct if there are duplicates.
Have a go at making the joined table first so that it gets you all the info you need to insert into company, and post up your query if you get stuck.
Cheers will give that a go first
When doing the insert into the company table do i need to get the company name and search that in the import table and then using the import table match the row within the address table? Cos currently company table is blank
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.