简体   繁体   中英

Query to delete lines from an outdated table based on ids of another table

I have a table that is getting bigger and bigger. It stores path of images of the products that are posted in my website, but when we delete the products, the system does not delete the images lines from the image table. So I built a list of IDs of products that are currently published in my site, and I would like to build a mysql query that deletes lines where there is no corresponding product published in my site any more, so I tried to use this, but this deletes every line of the table and not just the ones I need to delete, can somebody tell me what I am doing wrong?

Delete from my_table where product_id not like ( "67" or
"201" or
"329" or
"333" or
"334" or
"335" or
"340" or
"343" or
"347" or
"348" or
"458" )

...
...
...

This list has about 2000 entries... I just pasted some of them here. The problem is that this query deletes everything from my table and not just the lines where product_id is not one of the product ids I am listing.. I hope I managed to explain myself.

在ids列表中使用单引号而不是双引号而不是or

Delete from my_table where product_id not in ( '67', '201', '329', ...

You should try with:

DELETE FROM my_table
WHERE
  product_id NOT IN ("67", "201", "329", ...)

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