3

I have been looking at a lot of solutions, but I simply do not get it. What I want to do is remove 2 rows, each in a different table:

DELETE FROM adv_equip, adv_players 
WHERE session = '9746qbcinll2edfbmkfn316lu0'

There is a session field in both tables.

A lot of examples include t1, t2 and whatnot, and I do not know how to use it. I tried for example:

DELETE FROM adv_equip t1, adv_players t2 
USING t1, t2 
WHERE t1.session = t2.session 
AND t1.session = '9746qbcinll2edfbmkfn316lu0'

What should I do? It's been days and I kind find a logical easy solution anywhere.

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked Oct 23, 2016 at 12:53
1
  • If the rows are always tied together, consider FOREIGN KEYs and a "cascading delete". Commented Oct 24, 2016 at 2:10

1 Answer 1

3

The multi-table delete syntax goes like this:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
 tbl_name[.*] [, tbl_name[.*]] ...
 FROM table_references
 [WHERE where_condition]

or like this:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
 FROM tbl_name[.*] [, tbl_name[.*]] ...
 USING table_references
 [WHERE where_condition]

Source: MySQL documentation.

The documentation also points out in the Multi-Table Deletes section:

Table aliases in a multiple-table DELETE should be declared only in the table_references part of the statement.

That is what is wrong with your second example in particular. Instead of

DELETE FROM adv_equip t1, adv_players t2
USING t1, t2
...

it should be

DELETE FROM t1, t2
USING adv_equip t1, adv_players t2
...

YpercubeTM has raised a few good points in the comments. Since one of the conditions in your WHERE clause is effectively a joining condition, you might want to express the join more explicitly in your statement:

DELETE FROM
 t1, t2
USING
 adv_equip AS t1 INNER JOIN adv_players AS t2
 ON t1.session = t2.session
WHERE
 t1.session = '9746qbcinll2edfbmkfn316lu0'
;

A join, however, comes more naturally as part of a FROM clause, so you might find the first variation of MySQL's multi-table DELETE extension more consistent with an equivalent SELECT. This is how it would look in your case:

DELETE
 t1, t2
FROM
 adv_equip AS t1 INNER JOIN adv_players AS t2
 ON t1.session = t2.session
WHERE
 t1.session = '9746qbcinll2edfbmkfn316lu0'
;

I would probably also prefer specifying .* after the target names in the DELETE clause with that syntax:

DELETE
 t1.*, t2.*
...

That, in my opinion, would more clearly be conveying the intention: delete rows rather than the tables themselves.

And last, but not least, point from the comments is that if only one of the tables has the matching rows, your DELETE statement will fail to delete any rows at all. That is very unlike two separate statements each deleting from one table at a time and making sure that in the end neither has the specified rows. Depending on the intended outcome, therefore, you might prefer two DELETE statements to one.

answered Oct 23, 2016 at 14:25
1
  • Thanks Andriy!the MySQL docs tend to get too technical for my taste and I end up throwing it against the wall in a freak tantrum. Sometimes one needs to have it explained through more humane channels. :) Commented Oct 23, 2016 at 14:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.