简体   繁体   中英

MySQL: Deleting “orphaned” rows during a table update?

Ok...here is my issue...I'm pulling a few thousand records from a cloud REST query and updating a local table during a CRON and inserting and updating existing records with the pulled data. ie

INSERT INTO `property` ($columns_string) 
VALUES ($values_string) ON DUPLICATE KEY UPDATE $update_string

pretty standard stuff...but I'm having an issue where records are being "orphaned" if they don't exist any more in the pulled data ie I need to either delete existing records that no longer exist in the new data or update a flag or something to tell my system to no longer process them the same way.

I hope i'm being clear...

EDIT: I should add that the REST pull is actually looping thru several different "types" of data and the INSERT is stuffing them all into one master table, so a simple "delete if not exists.." won't really work..

advTHANKSance, - mark

Add a column "flag tinyint(1)" in table "property". Before uploading update column value to 0

update `property`
set flag = 0

In "insert into" clause update this new column

INSERT INTO `property` ($columns_string, flag) 
VALUES ($values_string, 1) ON DUPLICATE KEY UPDATE $update_string, flag=1

And after uploading remove/update unused field

update `property`
set active=0
where flag=0

In case if field "active" means 1 - as active, and 0 - is non active

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM