0

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 the features table to be 1, only if the corresponding images.uploaded value for that associated image is 0.

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 joins).

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!

asked Dec 31, 2019 at 3:40
1
  • 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/… Commented Dec 31, 2019 at 14:07

1 Answer 1

0

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 :)

answered Dec 31, 2019 at 19:35
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.