brooksh Posted March 12, 2007 Share Posted March 12, 2007 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 https://forums.phpfreaks.com/topic/42393-insert-data-from-one-table-to-another/ Share on other sites More sharing options...
brooksh Posted March 12, 2007 Author Share Posted March 12, 2007 I found this, but it doesn't work. Anyone have any ideas? mysql_query("INSERT INTO newnames (id, name) SELECT id, name FROM names"); Link to comment https://forums.phpfreaks.com/topic/42393-insert-data-from-one-table-to-another/#findComment-205725 Share on other sites More sharing options...
aebstract Posted March 12, 2007 Share Posted March 12, 2007 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 https://forums.phpfreaks.com/topic/42393-insert-data-from-one-table-to-another/#findComment-205738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.