populate many to many table from existing tables
database, many-to-many, sql
Solution
If you want to populate table A_B, you'd have to do this:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A CROSS JOIN B
CROSS JOIN will relate each row in table A with each row in table B.
If you want to relate some rows in table A with some rows in table B, you need to be more specific, and do something like:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A INNER JOIN B
ON A.some_field = B.some_other_field
Problem
I have two existing tables with data populated table A --tableA_id --contentA table B --tableB_id --contentB Now, I want to create a many to many relationship table table A_B --tableA_id --tableB_id the question is that how to write a sql script (I am new to sql) to populate table A_B using the existing data from table A and table B. Many thanks, Mark