Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with a MySQL query

Tags:

mysql

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

like image 857
HyderA Avatar asked Jan 22 '23 13:01

HyderA


2 Answers

DELETE FROM locks 
WHERE session_id IN (SELECT id FROM sessions WHERE user_id = 14)
like image 99
Kris C Avatar answered Jan 29 '23 10:01

Kris C


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

like image 30
JohnM Avatar answered Jan 29 '23 10:01

JohnM