为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL DELETE JOIN语句

标签:
MySQL

Summary: in this tutorial, we’ll show you how to delete data from multiple tables by using MySQL DELETE JOIN statement.

In the previous tutorial, we showed you several ways to delete records from multiple tables by using:

  • A single DELETE statement on multiple tables.

  • A single DELETE statement on multiple tables where the child tables have ON DELETE CASCADE referential actions for the foreign keys.

This tutorial introduces to you a more flexible way to delete data from multiple tables by using INNER JOIN or LEFT JOIN clause in the DELETE statement.

It is easier to follow this tutorial if you already have some knowledge of the INNER JOIN and LEFT JOIN clauses.

MySQL DELETE JOIN with INNER JOIN

You often use the INNER JOIN clause in the SELECT statement to select records from a table that have corresponding records in other tables. To make it more convenient, MySQL also allows you to use the INNER JOINclause with the DELETE statement to delete records from a table and also the corresponding records in other tables e.g., to delete records from both T1 and T2 tables that meet a particular condition, you use the following statement:

DELETE T1, T2 FROM T1 INNER JOIN T2 ON T1.key = T2.key WHERE condition

Notice that you put table names T1 and T2 between DELETE and FROM. If you omit the T1 table, the DELETEstatement only deletes records in the T2 table, and if you omit the T2 table, only records in the T1 table are deleted.

The join condition T1.key = T2.key specifies the corresponding records in the T2 table that need be deleted.

The condition in the WHERE clause specifies which records in the T1 and T2 that need to be deleted.

MySQL DELETE JOIN with INNER JOIN example

Let’s take a look at the following diagram.

MySQL DELETE with INNER JOIN

Each office has one or more employees, however each employee only belongs to on office.

Suppose you want to delete the office with officeCode 5 and you don’t update or delete the officeCode column in the employees table, the data in the employees table would be invalid. To delete the office with officeCode 5 and also employee records that belong to the office, you can use the DELETE statement with the INNER JOIN clause.

Let’s check the offices and employees data before deleting the data.

SELECT * FROM offices WHERE officeCode = 5

Office 5

SELECT * FROM employees WHERE officeCode = 5

Employees in office 5

Now, you can delete records associated with officeCode 5 from both offices and employees tables:

DELETE offices, employees  FROM offices INNER JOIN employees        ON employees.officeCode = employees.officeCode WHERE offices.officeCode = 5

If you use the SELECT statements above to query office and employee data with officeCode 5 in the offices and employees tables again, you will not see any row returned.

MySQL DELETE JOIN with LEFT JOIN

You often use LEFT JOIN clause in the SELECT statement to find records that exist in the left table and does not have corresponding records in the right table. You can also use the LEFT JOIN clause in the DELETE statement to delete record in a table (left table) that does not have corresponding record in another table (right table).

The following syntax illustrates how to use DELETE statement with LEFT JOIN clause to delete records from T1 table that does not have corresponding records in the T2 table:

DELETE T1 FROM T1  LEFT JOIN T2 ON T1.key = T2.key  WHERE T2.key IS NULL

Note that you only put T1 table after the DELETE keyword, not both T1 and T2 tables like you did with the INNER JOIN clause.

MySQL DELETE JOIN with LEFT JOIN example

Let’s see the following database diagram:

Customers and Orders Tables

Each customer has zero or more orders. However, each order belongs to one and only one customer.

You can use DELETE statement with LEFT JOIN clause to clean up our customers master data. The following statement removes customers who do not have any order:

DELETE customers FROM customers LEFT JOIN orders ON customers.customerNumber = orders.customerNumber WHERE orderNumber IS NULL;

Now, if we query customers who have not ordered any product by using the following SELECT LEFT JOIN query:

SELECT c.customerNumber,           c.customerName,           orderNumber FROM customers c LEFT JOIN orders o ON c.customerNumber = o.customerNumber WHERE orderNumber IS NULL;

You will not see any row returned. It means you have successfully deleted customers who have not ordered any products.

In this tutorial, You have learn how to use the MySQL DELETE JOIN with INNER JOIN  and LEFT JOIN  to delete data from multiple tables.

原文链接:http://outofmemory.cn/mysql/mysql-delete-join

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消