Forum Moderators: open
I have two tables, table1 and table2. The number of columns between the two tables are different from each other. Both tables have totally different fields.
I need to copy certain fields from 20,000 rows from table1 into table2. Could someone please point me to the correct SQL syntax to accomplish this..?
Currently, I use a while loop through PHP to do the copying.. But, I think this is not the best optimized solution?
TIA
[edited by: HoboTraveler at 7:23 pm (utc) on Sep. 25, 2006]
2/ Do one SQL query to get fields value from Table1 that need to be copied to table2. Store the output in an array.
3/ Print the array to see what you get is correct.
4/ Loop through the array using PHP and do an UPDATE query for each element of the array.
Can't help you more, unless you give more info about your table structure and what you want to update.
Will all the 50,000 rows get copied over successfully? What happens to the new data that is inserted into the source table during the copying process.
TIA
A good start would be to identify the potential errors that could happen and code it to write to the event log or a text log log when it runs then read the log to see what happennd. After a few test runs use the log to fix the code so you have some confidence being making it live.
Here is the script that I currently use to copy the list of emails from one table into the other. The table structure between the two tables are different.
The number of rows in the table are about 25,000. My concern is, is this the best way to do the copying and how do I ensure integrity that all the 25,000 records have been copied over correctly..?
TIA
-- begin code--
<?php
// Select the emails from the users table
$SqlSelectQuery =
("
SELECT
email
FROM
{$table_users}
WHERE
email_enabled = '1'
");
// Perform Query
$SqlSelectResult = mysql_query($SqlSelectQuery);
// Check result. This shows the Error
if (!$SqlSelectResult)
{
$SqlSelectMessage = 'Invalid query: ' . mysql_error() . "\n";
die($SqlSelectMessage);
}
// If no emails found, exit
$SqlSelectResultMatch = mysql_numrows($SqlSelectResult);
if ($SqlSelectResultMatch == 0)
{
echo 'Exit! Emails were not found .. Script Halted!<br>';
exit();
}
// If emails were found, proceed to copy emails
while ($SqlSelectRow = mysql_fetch_assoc ($SqlSelectResult))
{
// Store the email var
$email = $SqlSelectRow['email'];
// Copy the emails into the logs table
$SqlInsertQuery =
("
INSERT INTO
{$table_logs}
(
`dateInserted`,
`email`
)
VALUES
(
NOW(),
'$email'
)
");
$SqlInsertQueryResult = mysql_query($SqlInsertQuery);
// Check result. This shows the Error
if (!$SqlInsertQueryResult)
{
$SqlInsertQueryResultMessage = 'Invalid query: ' . mysql_error() . "\n";
die($SqlInsertQueryResultMessage);
}
$nextID = mysql_insert_id();
if ($nextID)
{
// proceed
}
else
{
echo 'Error! INSERT Error .. Script Halted!<br>';
exit();
}
}
?>
-- end code--
[edited by: HoboTraveler at 5:39 am (utc) on Oct. 3, 2006]