Jamal Lanten
Jamal Lanten

Reputation: 69

SQL Delete statement using JOIN

I need to DELETE rows from a MySQL table based on a JOIN.

Table 1 - players

id      gang
--------------
1       5
2       8
3       0

Table 2 - actions

id      player
----------------
1       2
2       1

I need to (pseudo code)

DELETE FROM 'actions' WHERE player=(SELECT id FROM players WHERE gang=5)

So it checks through actions table and if it finds a player that is in gang 5 it removes the entry.

Sorry if I'm not making sense

Upvotes: 0

Views: 1032

Answers (2)

Houari
Houari

Reputation: 5621

DELETE FROM 'actions' WHERE player IN (SELECT id FROM players WHERE gang=5)

Upvotes: 0

John Woo
John Woo

Reputation: 263693

Try this one,

DELETE  a
FROM    Actions a
        INNER JOIN Players b
            ON a.player = b.ID
WHERE   b.gang = 5

Upvotes: 1

Related Questions