mysqlsqldelete-rowmulti-table-delete

Can I delete rows from 2 tables in one single SQL statement?


I have 2 tables one called "REVIEW" and the other "REVIEW_DESCRIPTION"

If I do a join to get all the data from both:

SELECT *
FROM reviews 
INNER JOIN reviews_description
ON reviews.reviews_id=reviews_description.reviews_id
WHERE reviews.customers_id = 54183

Based on this I would like to delete rows in both tables that match this criteria in one shot, any tips?


Solution

  • Yes, MySQL supports deletions from more than one table in a single statement:

    For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching...

    First multi-table syntax example:

    DELETE reviews, reviews_description 
      FROM reviews r
      JOIN reviews_description rd
     WHERE reviews.reviews_id = reviews_description.reviews_id
       AND reviews.customers_id = 54183