HyderA
HyderA

Reputation: 21411

Need help with a MySQL query

Please help me with this MySQL query. I've been on it for long enough. Perhaps it needs a fresh pair of eyes.

Two tables: locks and sessions

locks
--------------
id session_id
--------------
1  sajf4$Jf9422jd
2  2jf*4j2okg9092
3  J8j4j4ffss93o2
------------------

sessions
-------------------------
id              user_id
-------------------------
sajf4$Jf9422jd  14
J8j4j4ffss93o2  14
2jf*4j2okg9092  21
-------------------------

I want to delete all rows in locks where user_id of session = 14

Upvotes: 4

Views: 102

Answers (5)

Kris C
Kris C

Reputation: 2848

DELETE FROM locks 
WHERE session_id IN (SELECT id FROM sessions WHERE user_id = 14)

Upvotes: 4

Omar Ali
Omar Ali

Reputation: 8617

DELETE locks, sessions FROM locks INNER JOIN sessions WHERE locks.session_id=sessions.id;

Upvotes: 0

Edward
Edward

Reputation: 29

Another way without a subquery:

delete from locks using locks join sessions on sessions.id=locks.session_id where sessions.user_id=14

Upvotes: 1

spbfox
spbfox

Reputation: 949

delete from locks where session_id in (select_id from sessions where user_id =14)

Upvotes: 2

JohnM
JohnM

Reputation: 161

DELETE FROM locks WHERE session_id = (SELECT id FROM sessions WHERE user_id = 14);

Upvotes: 2

Related Questions