在mySQL中删除具有多个产品记录的用户

时间:2011-12-16 18:52:44

标签: mysql sql-delete

有一个名为user_products的表,其中包含用户已获得许可的每个产品的记录。

user_id,product_id,(其他一些字段,如时间戳)

我有18个产品,但想找到所有拥有产品3记录和产品4记录的用户,然后删除他们对产品3的记录。

1 个答案:

答案 0 :(得分:2)

DELETE 
FROM user_products AS up3
WHERE product_id = 3
  AND EXISTS 
      ( SELECT *
        FROM user_products AS up4
        WHERE up4.product_id = 4
          AND up4.user_id = up3.user_id
      )

JOIN

DELETE up3
FROM user_products AS up3
  JOIN user_products AS up4
    ON up4.user_id = up3.user_id
WHERE up3.product_id = 3
  AND up4.product_id = 4