I have two MySQL tables. The first table has a cron job running where it gets updated every five minutes. When the cron job runs, it moves all the data from the first table into the second table (an archive table), truncates the first table and then updates the information from a CSV file that's constantly being updated at a certain URL. I made the tables by copying the structure from one table to the next (so they are identical in structure).
Here's the code I'm running, which works:
$sql = "
INSERT INTO table2 (id, time, size, location, county, state, lat, lon, comments)
SELECT id, time, size, location, county, state, lat, lon, comments FROM table1";
$result = $mysqli->query($sql);
The problem is, the cron job runs so often that it's not always going to be completely updated so it would make duplicate entries into table2. Here's the code I was trying to use, but it doesn't seem to be working:
$sql = "
INSERT INTO table2 (id, time, size, location, county, state, lat, lon, comments)
SELECT id, time, size, location, county, state, lat, lon, comments FROM table1
ON DUPLICATE KEY UPDATE id=id
";
$result = $mysqli->query($sql);
I've made sure the keys for both tables are the same (id).
Do these operations not working together or is my syntax wrong somewhere along the line? I wanted to try to run these operations all in one shot instead of updating the table and then running a different operation to get rid of the duplicates afterwards.
Thanks for your help!