da1lbi3 da1lbi3 - 4 months ago 5
SQL Question

Delete rows from table when the result are not in a csv file

I have this code to insert values from a csv file. Some values are updated when the row already exist at the database. Just how I want it to be.

INSERT INTO test_table (`id`,`order_id`,`quantity`,`price`,`order_date`,`shipping_date`)
VALUES ('$orderId', '$quantity', '$price', '$orderDate', '$shippingDate')
ON DUPLICATE KEY UPDATE `order_date` = '$orderDate', `shipping_date` = '$shippingDate'"


But when the row is not at the csv it must be deleted from the database.
For example:

csv file:

order_id quantity price order_date shipping_date
1 10 1.00 2016-10-5 2016-10-6


I run the script and the result from the csv is added to the database. But my next csv contains this:

order_id quantity price order_date shipping_date
2 120 2.00 2016-10-8 2016-10-10


A new row must be added. No problem, my query is fine. But the row with order_id 1 is gone at the csv. That row must be deleted from the database. How can I do this? Is this possible to do with my query? Or do I need another one?

Answer

You will need to add a field to store "version" of your update.

ALTER  TABLE test_table ADD version BIGINT DEFAULT 0;

Before doing INSERT , assign $vers = time() in PHP. Old rows will contain version value smaller than current version number.

Change your SQL to update version field in both INSERT and ON DUPLICATE parts of your query.

INSERT INTO test_table (`id`,`order_id`,`quantity`,`price`,`order_date`,`shipping_date`,`version`)
                    VALUES ('$orderId', '$quantity', '$price', '$orderDate', '$shippingDate','$vers')
                    ON DUPLICATE KEY UPDATE `order_date` = '$orderDate', `shipping_date` = '$shippingDate', `version` = $vers"

Do import.

Then after import is completed, delete old data

DELETE FROM test_table WHERE version < $vers

Should do the trick.