copying multiple row

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mosfet83
Forum Newbie
Posts: 4
Joined: Tue Apr 15, 2008 12:04 am

copying multiple row

Post by mosfet83 »

Hi again :P

i need some advise on how to copy multiple row with mysql and php
first way i can think of is sql statement as below

Code: Select all

INSERT INTO [i]table [/i](column1, column3) SELECT column2, column4 FROM [i]table2 [/i]
but how can i insert my own value into column3 in the same statement? do i have to insert column1 first then use UPDATE to insert column2 value?

second way maybe as below?

Code: Select all

 
$sql = "SELECT * FROM [i]table1[/i]";
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)){
   $sql2 = "INSERT INTO [i]table1[/i](column1, column2) VALUE ($row['name'], $row['phone']);
   $result2 = mysql_query($sql2,$db); 
}
 
which one is better? please advise. i have about 4000-10,000 rows per month to be duplicated with certain columns value needed to be changed. thank you.
Post Reply