sql - delete an entry from a table A if that entry is already present in a table B -


my question similar 1 sql delete rows based on table

except fact 2 entries match if 4 columns match!

so, have:

table1:

field        | type             | null | key | default | f1           | int(32) unsigned | no   | pri | null    | auto_increment f2           | int(32)          | no   |     | 0       | f3           | int(32)          | no   |     | 0       |  f4           | int(32)          | no   |     | 0       |                

and table f1,f2,f3,f4 + other fields. want delete record in table 1 have correspondence in table 2 based on

t1.f1=t2.f1 , t1.f2=t2.f2 , t1.f3=t2.f3 , t1.f4=t2.f4 

what's efficient way? though of retrieving t1 records in program , make n selects on t2, there average of 5k records each time in t1 , feel not efficient way...

edit: there lot of records made index, on t2, on 1 single field (say field1 argument's sake)

you use exists:

delete table1 t1 exists (     select 1 table2 t2     t1.f1=t2.f1 , t1.f2=t2.f2 , t1.f3=t2.f3 , t1.f4=t2.f4 ) 

the efficient approach depends on rdbms , indexes used.


Comments