I'm a bit confused about the proper way to update one table, based on the values of corresponding rows in another table.
Here is the query I have:
UPDATE
features,
images
SET
features.deleted = 1
WHERE
features.objectType = 'image'
AND images.id = features.objectId
AND images.uploaded = 0;
My goal:
I want to set the
features.deleted
column value for all rows in thefeatures
table to be1
, only if the correspondingimages.uploaded
value for that associated image is0
.
Business logic:
I want to go over all the images that been created in my app, and if the image wasn't successfully uploaded, I want to delete the corresponding
features
record.
I believe this works, but have been having a hard time finding specific examples using my approach (most suggest using join
s).
My concern is that the UPDATE
query is too general: I just want to make sure that it will only update feature
records that have their associated image
record with an uploaded
value of 0
.
Thanks for any guidance/confirmation!
-
It would help if you can provide the table definition (SHOW CREATE TABLE) for features and images tables. However, the answer may be similar to the one provided here - stackoverflow.com/questions/1262786/…jerichorivera– jerichorivera2019年12月31日 14:07:46 +00:00Commented Dec 31, 2019 at 14:07
1 Answer 1
I was correct, and the query above worked as desired. For anyone stumbling on this in the future, the format seemed to work given my context.
UPDATE
features,
images
SET
features.deleted = 1
WHERE
features.objectType = 'image'
AND images.id = features.objectId
AND images.uploaded = 0;
Thanks for your time everyone :)