Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete row from table where match exists in second table

Tags:

I have Table A with the following values:

     +------+------+ | ID1  | ID2  | +------+------+ | 1689 | 1709 | | 1709 | 1689 | | 1782 | 1709 | | 1911 | 1247 | | 1247 | 1468 | | 1641 | 1468 | | 1316 | 1304 | | 1501 | 1934 | | 1934 | 1501 | | 1025 | 1101 | +------+------+ 

and another relation (Table B) with the following values:

 +------+------+ | ID1  | ID2  | +------+------+ | 1641 | 1468 | | 1911 | 1247 | +------+------+ 

I would like to delete all rows in Table A that appear in Table B (an exact match on ID1 and ID2). Seems simple in theory but I'm having no joy with the EXISTS statement or other approaches. I'm using SQLite.

Any suggestions greatly appreciated.

like image 998
amjo324 Avatar asked Feb 10 '13 02:02

amjo324


Video Answer


2 Answers

How about: (not too sure whether this works in SQLite)

DELETE FROM TableA WHERE EXISTS (SELECT *               FROM TableB               WHERE TableB.ID1 = TableA.ID1                 AND TableB.ID2 = TableA.ID2) 
like image 168
Bernhard Barker Avatar answered Sep 28 '22 06:09

Bernhard Barker


DELETE a FROM TableA a INNER JOIN TableB b ON a.ID1=b.ID1 AND a.ID2=b.ID2 
like image 38
Ridwan Galib Avatar answered Sep 28 '22 04:09

Ridwan Galib