mysql - Is it possible to delete the records having same foreign key in different table with single query? -
i need delete records in multiple table having same foreign key id single sql query. given example below,
table name : basetable id | column1 ---|--------- 1 | bcol1 2 | bcol2 table name : reftable1 id | basetableid | column1 ---|-------------|-------- 1 | 1 | rt1col1 2 | 1 | rt1col2 table name : reftable2 id | basetableid | column1 ---|-------------|-------- 1 | 2 | rt2col1 2 | 1 | rt2col2 table name : reftable3 id | basetableid | column1 ---|-------------|-------- 1 | 1 | rt3col1 2 | 2 | rt3col2
in above 3 reference table want delete records having basetableid=1 single mysql query. there possibilities please share idea
i think it:
delete r1, r2, r3 reftable1 r1 join reftable2 r2 join reftable3 r3 r1.basetableid = 1 , r2.basetableid = 1 , r3.basetableid = 1
if tables may not have matching rows, left join should it:
delete r1, r2, r3 basetable b left join reftable1 r1 on b.id = r1.basetableid left join reftable2 r2 on b.id = r2.basetableid left join reftable3 r3 on b.id = r3.basetableid b.id = 1
Comments
Post a Comment