Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Query by joining multiple columns

I have Table T1 with Column C1,C2 and Table T2 with Column C3,C4,C5. I would like delete records from T1 where C1 = C3 AND C2 = C4 and C5 = '123'. What will be the query I tried following

DELETE FROM T1 WHERE (C1,C2) = SELECT (C3,C4) FROM T2 WHERE C5 = '123'

but this is not working.

like image 298
Debopam Avatar asked Jan 15 '14 19:01

Debopam


People also ask

Can we use join IN delete query?

A DELETE statement can include JOIN operations. It can contain zero, one, or multiple JOIN operations. The DELETE removes records that satisfy the JOIN conditions.


1 Answers

There is no WHERE (x,y) = (a,b) syntax in SQL Server, sorry. This is how you perform a delete, based on a join, regardless of how many columns are involved in the join:

DELETE t1 
  FROM t1
  INNER JOIN t2
  ON t1.c1 = t2.c3
  AND t1.c2 = t2.c4
  WHERE t2.c5 = '123';
like image 123
Aaron Bertrand Avatar answered Sep 19 '22 02:09

Aaron Bertrand