Good morning, I’m trying to copy data from an sql table(tb2) into another sql table (tb1) and have the tb2 data append with the row in tb1 by the ID. They are in the same DB. This will only be a one time thing I won’t have to do this recurring. The table structures are as follows:
**Tb1**
ID
Name
address
**Tb2**
ID
State
Country
I have the following code which will copy all the data but adds it to the end of the table instead of matching the ID’s and placing them accordingly.
INSERT INTO Tb1 (state, country)
SELECT state,country
FROM tb2
If I add a WHERE statement I receive the error “unknown column tb1.id” in “WHERE clause”
It didn’t add records because you have the same ids on both tables. If you’re looking to update, that’s another situation all together (INSERT != UPDATE)
If you’re on mySQL, you can try ON DUPLICATE INSERT, but I don’t know if you can do that using a SELECT. For sanity’s sake, I’d split them into two transactions, but that’s just me…
actually you have a good point I am actually trying to update tb1 with the data from tb2 coinciding with their relative id’s. sorry for the confusion. that being said I’ve played around with a few UPDATE querys and still can’t get it to work. any ideas? thank you.
It was my fault for saying it was an insert instead of an update. I actually used a great deal of what you had sent me in order to figure it out. Thank you again for the help.