简体   繁体   English

MySQL:在表更新期间删除“孤立”行吗?

[英]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. 好的...这是我的问题...我要从云REST查询中提取几千条记录,并在CRON期间更新本地表,并使用提取的数据插入和更新现有记录。 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.. 编辑:我应该补充一点,REST拉实际上是通过几种不同的“类型”的数据循环,而INSERT则将它们全部塞进一个主表中,因此简单的“如果不存在则删除..”将不会真正起作用。

advTHANKSance, - mark advTHANKSance,-标记

Add a column "flag tinyint(1)" in table "property". 在表“属性”中添加列“ flag tinyint(1)”。 Before uploading update column value to 0 在将更新列值上传到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 如果字段“活动”表示1-为活动,而0-为非活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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