tags:

views:

124

answers:

5

how can i delete from table A while comparing two fields A.P and A.C to table B's B.P and B.C while looking for all matches of B.R = 1 ?

actually neither of the following work but it should go into the direction, unfortunately i can't figure out how...

DELETE FROM A WHERE (A.P = B.P AND A.C = B.C where B.C = 1)

DELETE FROM A WHERE (SELECT B.P, B.C FROM B WHERE B = 1)
+4  A: 
DELETE FROM A
FROM A INNER JOIN B ON A.P = B.P AND A.C = B.C
WHERE B.C = 1

The double FROM sometimes throws people off.

womp
unfortunately doesn't work with SQLite.
mgmx
+1  A: 
DELETE FROM A WHERE A.Id IN 
(SELECT A.Id FROM A INNER JOIN B ON A.P = B.P WHERE B.C = 1)
despart
A: 

You are asking to delete all records from A That have a C value = to the C Value in the row in table B that has C = 1 and the same P value??

That's the same as deleting all rows in A That have C value = 1 [and the same P Value in Table B].

so Try this:

Delete A 
Where C = 1 
   And Exists
      (Select * From B
       Where C = 1 
         And P = A.P)
Charles Bretana
A: 

something like this would work

DELETE FROM A
FROM A
 INNER JOIN B ON A.P = B.P AND A.C = B.C AND B.R = 1
Hogan
A: 
DELETE
FROM    A
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    b
        WHERE   b.p = a.p
                AND b.c = a.c
                AND b.r = 1
        )
Quassnoi
@Quassnoi: Wouldn't my query be faster?
Hogan
No, it won't. In `SQL Server` (which is the only `RDBMS` your query will work), on unindexed fields `JOIN` is less efficient than `IN` or `EXISTS`. If the fields are unique or indexed properly, then `JOIN` will be of same efficiency. http://explainextended.com/2009/06/16/in-vs-join-vs-exists/
Quassnoi
Actually this one works perfectly!!Also with SQLite...
mgmx