user5783530 user5783530 - 1 year ago 44
MySQL Question

Get all values from table and reuse them to create rows in other table

I have a table (People) with columns (id, name, year). Is there any way I can get all the ids (

SELECT id FROM people
) and use them for creating new rows in other table (people_id, additional_info). I mean that for every id in table People I want to add new row in other table containing the id and some additional information. Maybe something like for-loop is needed here ?

Answer Source

Well, usually you have information on a person and write one record with this information. It makes litte sense to write empty records for all persons. Moreover, when it's just one record per person, why not simply add an information column to the people table?

Anyway, you can create mock records thus:

insert into people_info (people_id, additional_info)
select id, null from people;