tags:

views:

40

answers:

3

I would like to delete all the rows found by that query:

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

The thing to keep in mind is that the query here uses 4 different tables, however I only want to delete rows from 1 table (cart_abandon).

Is there an easy way to do that? Maybe this: ?

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

Is that valid? Correct?

A: 

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

a1ex07
I did, and it really wasn't clear to me, which is why I came here ...
nute
But what I understood from it led me to the proposed query I wrote above - is that correct?
nute
It seems that your query is ok, you use the fist syntax (rows get deleted from the table[s] before FROM :DELETE cart_abandon FROM
a1ex07
I'd change it a bit by using INNER JOIN instead of listing all the tables in FROM , but it shouldn't make any difference for the server. It would be easy to read though...
a1ex07
A: 

You can use subqueries:

Would look like this:

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

Check this site for further informations: http://dev.mysql.com/doc/refman/5.1/en/delete.html

Prine
A: 
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
)
therealsix