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.

有帮助吗?

解决方案

You should try with:

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

其他提示

Use single quotes instead of double and commas in your list of ids instead of or

Delete from my_table where product_id not in ( '67', '201', '329', ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top