moving a selection of records from one table to another (identicalin structure) table?

moving a selection of records from one table to another (identicalin structure) table?

am 27.09.2006 12:44:23 von Evert

Hi all!

What is the best/easiest way to use PHP to move a selection of MySQL
records from one table to another table, which is in the same database
and which has the same structure as the first table?


Greetings,
Evert

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: moving a selection of records from one table to another (identical in structure) table?

am 27.09.2006 13:29:13 von Niel Archer

Hi

Easiest way would be to have MySQL to do it, using an INSERT ... SELECT
statement

Niel

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: moving a selection of records from one table to another (identical in structure) table?

am 27.09.2006 13:31:58 von Niel Archer

Hi

> Easiest way would be to have MySQL to do it, using an INSERT ... SELECT
> statement

.. and if you're moving (not just copying) deleting the selected
records after of course.

Niel

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: moving a selection of records from one table to another (identicalinstructure) table?

am 27.09.2006 14:06:11 von Kae Verens

Evert wrote:
> Hi all!
>
> What is the best/easiest way to use PHP to move a selection of MySQL
> records from one table to another table, which is in the same database
> and which has the same structure as the first table?

here's a stab at it (not tested):

$ids_to_get(1,3,5,6,8);
$q=mysql_query(
'select * from from_table
where id="'.join('" or id="',$ids_to_get).'"'
);
while($r=mysql_fetch_array($q)){
$fields=array();
foreach($r as $k=>$v)$fields[]='`'.$k.'`="'.addslashes($v);
echo('insert into to_table set '.join(',',$fields).'
');
# mysql_query('insert into to_table set '.join(',',$fields));
}


obviously, first try the above to make sure the SQL looks right, before you
uncomment the line that does the damage

Kae

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: moving a selection of records from one table to another (identicalinstructure) table?

am 27.09.2006 14:07:05 von Kae Verens

Evert wrote:
> Hi all!
>
> What is the best/easiest way to use PHP to move a selection of MySQL
> records from one table to another table, which is in the same database
> and which has the same structure as the first table?

here's a stab at it (not tested):

$ids_to_get(1,3,5,6,8);
$q=mysql_query(
'select * from from_table
where id="'.join('" or id="',$ids_to_get).'"'
);
while($r=mysql_fetch_array($q)){
$fields=array();
foreach($r as $k=>$v)$fields[]='`'.$k.'`="'.addslashes($v);
echo('insert into to_table set '.join(',',$fields).'
');
# mysql_query('insert into to_table set '.join(',',$fields));
}


obviously, first try the above to make sure the SQL looks right, before you
uncomment the line that does the damage

Kae

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: moving a selection of records from one table to another (identicalin structure) table?

am 27.09.2006 14:27:35 von Evert

Would you happen to have a nice little example-script for me/us? :-)

Regards,
Evert

Niel Archer wrote:
> Hi
>
>> Easiest way would be to have MySQL to do it, using an INSERT ... SELECT
>> statement
>
> ... and if you're moving (not just copying) deleting the selected
> records after of course.
>
> Niel
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: moving a selection of records from one table to another (identical in structure) table?

am 27.09.2006 14:58:52 von Niel Archer

Hi

> Would you happen to have a nice little example-script for me/us? :-)

yup, here's a little example:

$condition = "WHERE username LIKE 'm%'"
if (mysql_query("INSERT AuthCopy SELECT * FROM Auth $condition"))
mysql_query("DELETE FROM Auth $condition");

This assumes the two tables are identical with regard to column
order/type but not name, and that you want to copy and remove the data
from the first table.
The whole operation is performed by the MySQL server, so requires almost
zero processing by PHP and should be faster. However, it also means you
don't get to verify each insert as it is made, but that should not be a
major problem.


Niel

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php