![]() |
Loading data form table to table?
I am having a problem with the last bit of my code. I am trying to make a script that will load data from one table and insert it into another table in the same DB. This is what I got.
PHP Code:
|
Your original query was not working, thou it was only some minor mistakes that caused it to fail. I suggest you take a look at the MySQL Reference on the INSERT syntax.
The following code should work, but note that I have not tested it myself. It will first attempt to copy all records from the table`online_report` to the table `call_in`. Then it will return one message; one for success (if affected rows is higher than 0) or one for failure. PHP Code:
Yours, Runar |
That did not work. Get an error of " Duplicate entry '600-15691869000' for key 1 ". Can I make it ignore that error and insert the data anyway?
|
Since I am not familiar with your database structure, I will have to guess on what exactly causes the problem. Is the table you are copying to empty, or does it contain existing data? Does the entry '600-15691869000' exist in both tables?
If it does contain data, you will either have to delete that data before copying from the other table, or make sure you do not attempt to copy the entries that exist in both tables. I may be wrong, but give us some more information about your database structure and existing data, and I am sure someone will be able to help you. |
As for the '600-15691869000', I'm not sure what the '600-' part is because thats not in ant of my tables. And for '15691869000' it is a driver number so it will be used many time in both tables.
Let me go over what I am doing. I have a system that records data about truck drivers for when they arrive and depart from their stops. They can either call it in over the phone to an automated system that writes to 1 table OR they call our operator who will enter the data into a different table. Each company that uses our system can check online to monitor the drivers logs. For the automated system, its data get stored on a different server and sent to the web server (kinda ridiculous if you ask me, but I didn't sent it up), but the catch is it just continues to write to the same file so when it copies to the web server it send the same data plus what ever was added. Then this is what happens: PHP Code:
|
I apologize. SQL in general is far from my favourite field, so I am not sure if this will help you or not. You could use the following method to only copy certain fields between the two tables, like for cases when one or both of the tables contains unique fields. Maybe that is what you are looking for:
Code:
INSERT `call_in` (`time`, `date`, `company_id`, <more fields>) SELECT `time`, `date`, `company_id`, <more fields> FROM `online_report` |
| All times are GMT. The time now is 07:28 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0