代码之家  ›  专栏  ›  技术社区  ›  Nathan H

mysql从一个表中删除,使用多个表

  •  0
  • Nathan H  · 技术社区  · 16 年前

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

    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_above)。

    有什么简单的方法可以做到吗?也许这个:?

    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 回复  |  直到 16 年前
        1
  •  0
  •   a1ex07    16 年前

    检查delete multiple table on syntax http://dev.mysql.com/doc/refman/5.1/en/delete.html

        2
  •  0
  •   Prine    16 年前

    你可以使用 子查询 :

    会是这样的:

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

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

        3
  •  0
  •   therealsix    16 年前
    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
    )