Jump to content

Insert data from one table to another


brooksh

Recommended Posts

First of all, I want to know if this script will even work. I only want it inserted if it doesn't already exist. I know that I could do replace, but I think it would use more of the servers resources. This is about 8000 rows from the first table. And if it is possible to make it more efficient.

 

	
$sql = "select id,name from names";
        $result = mysql_query ($sql);
            while($data = mysql_fetch_array ($result, MYSQL_ASSOC))
        {
$newid = $data[id];
$newname = $data[name];
mysql_query("INSERT into newnames SET id=$newid, name=$newname WHERE id > $newid");
}

Link to comment
Share on other sites

I have trouble reading other people's code, I don't know why.. it's just the way I have always been. Though I would like to try and explain how I would get this accomplished, and if you think you can do that go for it.

 

First you would want to grab all information from the first one, then run a while for that query, in the while you should tell it to check the other table for the information given, if it exists do nothing, if it doesn't.. then place the information in (use an if for that). Since you're running it in a while, it should go through and do this for every line untill it is finished.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.