Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop in SQL query

Tags:

sql

Basically I want to extract id from a table and insert that id in another table with some other data. So the algorythm goes:

foreach id in (select id from table)
{
    insert into table2 values (id,etc,etc);
}

How to perform this function with an SQL query?

like image 549
faraz ali Avatar asked Jan 19 '23 06:01

faraz ali


1 Answers

Use

insert into table2 
select id, etc, etc from table

instead of for loop.

Here's a helpful article.

like image 104
soulcheck Avatar answered Jan 25 '23 23:01

soulcheck