有一个名为user_products的表,其中包含用户已获得许可的每个产品的记录。
user_id,product_id,(其他一些字段,如时间戳)
我有18个产品,但想找到所有拥有产品3记录和产品4记录的用户,然后删除他们对产品3的记录。
答案 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