I have 3 tables and I need to delete some rows from them. Table2 has a many-to-one relationship with Table1 and Table 3 has a many-to one relationship with Table2. They look like this:
Table1
ServiceId (pk)
Table2
ServiceId (fk)
ElementId (pk)
Table3
ElementId (fk)
Of course I've left out all the non-key data. So now I need to delete all of the rows in all three tables that correspond to a particular Service ID.
It's simple enough to run something like DELETE FROM Table WHERE ServiceId = $servId
Where $servId is a PHP variable containing the ID of the service I want to delete.
But for Table3, there's no service ID so I can't just delete rows. Basically I want to delete all rows in Table3 where there is a corresponding ElementId in Table2
My question is, would something like this work? And if not, how should I do it?
DELETE FROM Table3 WHERE Table3.ElementId=Table2.ElementId AND Table2.ServiceId=$servId
Any thoughts? Thanks.