Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL - Delete Query with Join

Tags:

mysql

I have checked the answered questions. But, solutions are not working for me.

DELETE FROM TEST2
INNER JOIN TEST1 on TEST1.FIELD2 = TEST2.FIELD2
WHERE TEST1.FIELD1 = 22;

When i execute this query, i am getting the following error in phpmyadmin.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 
for the right syntax to use near 'INNER JOIN TEST1 on TEST1.FIELD2 = TEST2.FIELD2' at line 2

I am getting tired. I need help. Thanks in advance.

like image 438
user2003356 Avatar asked Feb 22 '13 14:02

user2003356


1 Answers

This should work:

DELETE T
FROM TEST2 T
INNER JOIN TEST1 on TEST1.FIELD2 = T.FIELD2
WHERE TEST1.FIELD1 = 22;

Sample Fiddle Demo

I think you can also do it with IN:

DELETE FROM Test2
WHERE Field2 IN (
    SELECT Field2 
    FROM Test1
    WHERE Field1 = 22)
like image 139
sgeddes Avatar answered Oct 13 '22 10:10

sgeddes