MySQL从1个表中删除,使用多个表

时间:2010-04-14 22:46:55

标签: sql mysql

我想删除该查询找到的所有行:

SELECT cart_abandon.*
FROM cart_abandon, cart_product, txn_product, users
WHERE cart_abandon.cartid = cart_product.cartid
AND cart_product.productid = txn_product.productid
AND txn_product.username = users.username
AND users.id = cart_abandon.userid
AND txn_product.txndate >= cart_abandon.abandondate

要记住的是这里的查询使用了4个不同的表,但我只想删除1个表中的行(cart_abandon)。

有一种简单的方法吗?也许这个:?

DELETE cart_abandon
FROM cart_abandon, cart_product, txn_product, users
WHERE cart_abandon.cartid = cart_product.cartid
AND cart_product.productid = txn_product.productid
AND txn_product.username = users.username
AND users.id = cart_abandon.userid
AND txn_product.txndate >= cart_abandon.abandondate

这有效吗?正确的吗?

3 个答案:

答案 0 :(得分:0)

选中语法http://dev.mysql.com/doc/refman/5.1/en/delete.html

上的删除多表格

答案 1 :(得分:0)

您可以使用子查询

看起来像这样:

DELETE FROM foo
WHERE EXISTS
(SELECT * from bar WHERE ..)

检查此网站以获取更多信息: http://dev.mysql.com/doc/refman/5.1/en/delete.html

答案 2 :(得分:0)

DELETE cart_abandon
WHERE cartid IN (
   SELECT cart_abandon.cartid
   FROM cart_abandon, cart_product, txn_product, users
   WHERE cart_abandon.cartid = cart_product.cartid
   AND cart_product.productid = txn_product.productid
   AND txn_product.username = users.username
   AND users.id = cart_abandon.userid
   AND txn_product.txndate >= cart_abandon.abandondate
)