4

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

5 Answers 5

4
DELETE FROM locks 
WHERE session_id IN (SELECT id FROM sessions WHERE user_id = 14)
Sign up to request clarification or add additional context in comments.

1 Comment

DELETE FROM locks WHERE session_id IN (SELECT id FROM sessions WHERE user_id = 14) but close enough
2

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

Comments

2

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

Comments

1

Another way without a subquery:

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

Comments

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

Comments

Your Answer

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.