Jump to content

Matching data in 2 tables and insering in 3rd table


grippat

Recommended Posts

I have two tables: Votes and Addresses. Votes contains just "fname" and "lname" fields. Addresses contains "fname", "lname", "address", etc. How can I use just MySQL to insert the data from Addresses into a 3rd table AddressesWithVotes that have matching fname and lname in the Votes table?

Link to comment
Share on other sites

I have two tables: Votes and Addresses. Votes contains just "fname" and "lname" fields. Addresses contains "fname", "lname", "address", etc. How can I use just MySQL to insert the data from Addresses into a 3rd table AddressesWithVotes that have matching fname and lname in the Votes table?
This works in MS SQL, and I assume MySQL too:
insert into addresseswithvotes (fname, lname, address)select a.fname, a.lname, a.address from votes vinner join addresses a on a.fname = v.fname and a.lname = v.lname

Normally you would have a Person record, and have PersonID rather than fname+lname on the other tables. In that case you would just use PersonID in the join. But anyway, that's the logic.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...