Current location: Hot Scripts Forums » Other Discussions » Database » Moving data to another mySQL table


Moving data to another mySQL table

Reply
  #1 (permalink)  
Old 10-04-06, 01:34 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Moving data to another mySQL table

I have two mySQL table with same coluumns.
The first table called: "files_temp" and second table called: "files"

I want to move data from "files_temp" to "files" and remove the moved data from "files_temp".

I do this with following mySQL querys:

PHP Code:

$msgfiles mysql_query("SELECT msg_id, userid, name, newname, size, type FROM files_temp WHERE msg_id='".$msgid."'") or die(mysql_error());

        
while (
$row mysql_fetch_array($msgfiles)) 
{
    
mysql_query("INSERT INTO files (id, msg_id, userid, name, newname, size, type) VALUES (0, '".$row['msg_id']."', '".$row['userid']."', '".$row['name']."', '".$row['newname']."', '".$row['size']."', '".$row['type']."')") or die(mysql_error());

Exist a better way to do this, to solve this problem with a single mySQL query?

Thanks !

Last edited by Christian; 10-04-06 at 04:57 PM.
Reply With Quote
  #2 (permalink)  
Old 10-04-06, 03:22 AM
websmart's Avatar
websmart websmart is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Moving Data from one to another Table

There is more proper way to do that. I had searched various options to
do that and finally settled on the following method:

PHP Code:

$qry1=mysql_query("INSERT INTO `files` SELECT * FROM `files_temp`");

$qry2=mysql_query("TRUNCATE TABLE `files_temp`"); 
First query will copy records from files_temp into table files
and then it erases all records from files_temp. This solution
works perfectly for my tables.


enjoy!
__________________
Vyapari Trade Secrets
http://www.vyapari.com/maillist/
Reply With Quote
  #3 (permalink)  
Old 10-04-06, 04:27 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
if you have phpmyadmin, simply rename the first table to the second table:

this function can be found in the "Operations" tab when you select a table in your database. Make sure you don't have a table called 'files' otherwie it wont rename

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #4 (permalink)  
Old 10-04-06, 05:53 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Assuming you had columns named 'title', 'hits', and 'userid', you could do this:

Code:
INSERT  INTO newtable 
SELECT title, hits, userid 
FROM oldtable WHERE title <>  '' 
ORDER  BY title;
No need for temp tables with this method and it's also very fast.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting data to mysql table using php Tjobbe PHP 4 09-28-06 06:37 AM
How to get PHP to input data into a MYSQL table? scl789 PHP 5 04-21-05 09:06 PM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM
moving data from table to table ..please help! geneane ASP 2 09-22-03 07:02 PM


All times are GMT -5. The time now is 04:56 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.