Forum Moderators: open

Message Too Old, No Replies

Easy way to add records from one MySQL table to another?

Trying to avoid select/insert for each record

         

MichaelBluejay

3:35 pm on Mar 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Every night I want to add the records from my <daily> table to my <longterm> table. Seems like there ought to be an easy way to do this. I'm trying to avoid what seems inefficient, in Perl:


$cursor = $DB->prepare("SELECT * FROM daily);
$cursor->execute;
while @data = ($cursor->fetchrow) {
$DB->do("INSERT into longterm values('$data[0]','$data[1]','$data[3]','$data[4]','$data[5]')");
}

Is there some simple MySQL command that does something like "add the records from table A to table B"?

marcmesa

6:34 pm on Mar 25, 2006 (gmt 0)

10+ Year Member



i'm a semi-nice into mysql but i think that a better way to do it is without having to make an insert for every row. Instead use one multiple-insert.

/*SQL query*/
INSERT INTO longterm
SELECT *
FROM daily

Execute the query the perl way (sorry, i don't know perl)

Hope it helps!

MichaelBluejay

7:30 pm on Mar 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Wow, that's great. With Perl all I have to do is:

$DB->do("INSERT into longterm SELECT * FROM daily");

This is exactly what I needed. Thanks!

marcmesa

7:50 pm on Mar 25, 2006 (gmt 0)

10+ Year Member



Glad to help you.

NOTE: in the other post it says semi-nice whre i would say semi-novice (newbie)

coopster

11:51 pm on Mar 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well then I'd have to say you were a very-nice semi-novice, marcmesa.
Welcome to WebmasterWorld ;)