MySQL delete multiple rows in multiple tables

acoder

Is there a way to delete multiple records from three tables at once in one query?

categories:
  id, name

sub_categories:
  id, category_id, name

items:
  id, subcategory_id, name

I have id of the category that I need to delete. For example, 5

The SQL query must delete the category with that id.

categories.id = 5

Also, it must delete all the subcategories from that category.

sub_categories.category_id = categories.id

And finally, delete all items from those subcategories that where removed in step 2.

items.subcategory_id = sub_categories.id
1000111

One way you can delete from multiple tables if you introduce foreign key constraints with ON DELETE CASCADE.

This is the other way around:

DELETE C,SC,I
FROM categories C 
INNER JOIN sub_categories SC ON C.id = SC.category_id
INNER JOIN items I ON SC.id = I.subcategory_id
WHERE C.id = 5;

Check this Delete with join(multiple tables)

EDIT:

If sub categories don't have any item under it then you need to replace the last INNER JOIN by LEFT JOIN

DELETE C,SC,I
FROM categories C 
INNER JOIN sub_categories SC ON C.id = SC.category_id
LEFT JOIN items I ON SC.id = I.subcategory_id
WHERE C.id = 5;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

Combine multiple rows from multiple tables in MySQL

From Dev

MySQL delete multiple rows if they exist

From Dev

Delete rows from multiple tables as one query

From Dev

Delete rows from multiple tables in a database

From Dev

How to delete multiple rows in diferent tables?

From Dev

How to delete rows in multiple tables using pdo

From Dev

How to delete multiple rows from multiple tables using Where clause?

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Conditional delete across multiple tables in mysql

From Dev

MySQL combine rows from multiple tables into on row

From Dev

Dynamic rows to columns from multiple tables with mysql

From Dev

MYSQL Select from tables based on multiple rows

From Dev

MySQL JOIN two tables and return multiple rows

From Dev

Can a MySQL event delete multiple rows instantly?

From Dev

mysql multiple table DELETE rows syntax error

From Dev

Stored procedure: delete rows from multiple tables with output for every row

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

Delete multiple rows in JavaScript

From Dev

Delete multiple rows in a ListVIew

From Dev

Mysql trigger on Delete to multiple tables (also error 1235)

From Dev

MySql: delete from multiple tables using left join

From Dev

How to delete from multiple tables with the same column in mysql?

From Dev

Extract rows from multiple MySQL tables based on date

From Dev

MySQL count rows from multiple tables using join

From Dev

Mysql select rows from multiple tables in specific order

From Dev

MySQL - Display multiple rows in one field (tables with inner joins)q

Related Related

  1. 1

    DELETE rows from multiple tables with JOIN in Mysql

  2. 2

    Delete multiple rows from multiple tables in mysql using single value

  3. 3

    Postgresql delete multiple rows from multiple tables

  4. 4

    Combine multiple rows from multiple tables in MySQL

  5. 5

    MySQL delete multiple rows if they exist

  6. 6

    Delete rows from multiple tables as one query

  7. 7

    Delete rows from multiple tables in a database

  8. 8

    How to delete multiple rows in diferent tables?

  9. 9

    How to delete rows in multiple tables using pdo

  10. 10

    How to delete multiple rows from multiple tables using Where clause?

  11. 11

    MySQL - Delete from multiple tables using a UNION?

  12. 12

    Conditional delete across multiple tables in mysql

  13. 13

    MySQL combine rows from multiple tables into on row

  14. 14

    Dynamic rows to columns from multiple tables with mysql

  15. 15

    MYSQL Select from tables based on multiple rows

  16. 16

    MySQL JOIN two tables and return multiple rows

  17. 17

    Can a MySQL event delete multiple rows instantly?

  18. 18

    mysql multiple table DELETE rows syntax error

  19. 19

    Stored procedure: delete rows from multiple tables with output for every row

  20. 20

    SQL Select rows and delete/update from dynamic multiple tables

  21. 21

    Delete multiple rows in JavaScript

  22. 22

    Delete multiple rows in a ListVIew

  23. 23

    Mysql trigger on Delete to multiple tables (also error 1235)

  24. 24

    MySql: delete from multiple tables using left join

  25. 25

    How to delete from multiple tables with the same column in mysql?

  26. 26

    Extract rows from multiple MySQL tables based on date

  27. 27

    MySQL count rows from multiple tables using join

  28. 28

    Mysql select rows from multiple tables in specific order

  29. 29

    MySQL - Display multiple rows in one field (tables with inner joins)q

HotTag

Archive