Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete using Inner Joins

So I have this correlated subquery, and I was reading that this is not the most efficient way to go about this. Hence, I want to convert this query to an INNER JOIN query.

DELETE FROM tableA
WHERE EXISTS (
           SELECT fieldA
           FROM tableB
           WHERE tableB.fieldA= tableA.fieldA)

I tried something like this:

DELETE a 
FROM TableA a
INNER JOIN TableB b
ON a.fieldA = b.fieldA

Which resulted in an Error while executing SQL query on database 'DB': near "a": syntax error

All of my search results on here yielded approximately the same query (similar to what I have tried)

like image 907
David Avatar asked Dec 20 '25 16:12

David


1 Answers

What you posted works fine for SQL Server; for MySQL below should do the job

DELETE tableA
FROM tableA
INNER JOIN tableB ON tableB.fieldA = tableA.fieldA;
like image 115
Rahul Avatar answered Dec 22 '25 04:12

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!