tags:

views:

66

answers:

4

I have the following select query

SELECT t1.*
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;

I want to convert this into a delete query. how should i write it?

+1  A: 

Not 100% on access but does:

DELETE t1
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;

Work?

Preet Sangha
no this doesnt work
tksy
The FROM clause is not valid for a Jet/SQL DELETE statement.
David-W-Fenton
@david-thank you
Preet Sangha
+1  A: 

Try this

DELETE FROM t1
FROM t1 AS tt1, t2 AS tt2
WHERE tt1.field1=tt2.field1 And tt1.field2=tt2.field2 And tt1.field3=tt2.field3 ;

EDIT:

Did this in MS Access

DELETE DISTINCTROW t1.*
    FROM t1 INNER JOIN t2 ON (t1.field3 = t2.field3) AND (t1.field2 = t2.field2) AND (t1.field1 = t2.field1);

And it worked, you have to set the Unique Records to Yes

astander
syntax error in from clause (on second line)
tksy
Shouldn't "DELETE FROM t1" in the first example be "DELETE FROM tt1"? And what's the purpose of aliasing that makes the table names longer?
David-W-Fenton
The second answer is the correct one, i left the first one but can remove it
astander
A: 

What about this:

DELETE 
FROM t1
INNER JOIN t2 ON t1.field1=t2.field1 
  And t1.field2=t2.field2 
  And t1.field3=t2.field3

Will delete all records in t1 that have a matching record in t2 based on the three field values.

patmortech
+2  A: 

What about this query:

DELETE FROM t1
WHERE t1.field1 IN (
  SELECT t1.field1 FROM t1, t2  
  WHERE t1.field1=t2.field1 And
        t1.field2=t2.field2 And
        t1.field3=t2.field3)
Nick D