第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

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

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消