0

我有三个表,productsshopssex。我希望DELETEproducts 表中的行具有这样的表中product_id不存在等效项。product_idsex

此外,表中的这些行的值products必须shop_id等于表中值为“www.shop.com”的行。shop_idshopsshops.shop

到目前为止我有

DELETE FROM products USING shops WHERE
products.shop_id=shops.shop_id AND
shops.shop='www.shop.com' AND NOT EXISTS
(SELECT sex.product_id FROM sex WHERE
sex.product_id=products.product_id)

但似乎products像我所做的那样在子查询中引用是不可接受的。(我得到错误Unknown table 'products' in MULTI DELETE。)我该如何解决我的错误?

4

1 回答 1

2

您可以在语句中使用JOINs :DELETE

DELETE    a 
FROM      products a
JOIN      shops b ON a.shop_id = b.shop_id AND b.shop = 'www.shop.com'
LEFT JOIN sex c ON a.product_id = c.product_id
WHERE     c.product_id IS NULL

这将DELETE仅适用于在shops表中具有shop= www.shop.com 的相应行的产品,但前提是该产品在表中也没有相应的行sex

于 2012-08-17T19:47:07.810 回答