亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

MySQL 子連接示例

標簽:
MySQL

Summaryin this tutorial, you will learn how to use MySQL self join that joins a table to itself using join statement.

In the previous tutorial, you have learned how to join a table to the other tables using INNER JOINLEFT JOIN or RIGHT JOIN statement. However, there is a special case that you can join a table to itself, which is known as self join.

You use self join when you want to combine records with other records in the same table. To perform the self join operation, you must use a table alias to help MySQL distinguish the left table from the right table of the same table.

MySQL Self Join Examples

Let’s take a look at the employees table in the sample database.

In the employees table, we store not only employees data but also organization structure data. The reportsto column is used to determine the manager ID of an employee.

mysql self join employees table

In order to get the whole organization structure, we can join the employees table to itself using the employeeNumberand reportsTo columns. The employees table has two roles: one is Manager and the other is Direct Report.

SELECT CONCAT(m.lastname,', ',m.firstname) AS 'Manager',        CONCAT(e.lastname,', ',e.firstname) AS 'Direct report' FROM employees e INNER JOIN employees m ON m.employeeNumber = e.reportsto ORDER BY manager

mysql self join example

In the above example, we only see employees who have manager. However, we don’t see the top manager because his name is filtered out due to the INNER JOIN clause. The top manager is the employee who does not have manager or his manager no. is NULL.

Let’s change the INNER JOIN clause to the LEFT JOIN clause in the query above to include the top manager. We also need to use the IFNULL function to display the top manager if the manger’s name is NULL.

SELECT IFNULL(CONCAT(m.lastname,', ',m.firstname),'Top Manager') AS 'Manager',        CONCAT(e.lastname,', ',e.firstname) AS 'Direct report' FROM employees e LEFT JOIN employees m ON m.employeeNumber = e.reportsto ORDER BY manager DESC

mysql self join with left join

By using MySQL self join, we can display a list of customers who locate in the same city by joining the  customerstable to itself.

SELECT c1.city,        c1.customerName,        c2.customerName FROM customers c1 INNER JOIN customers c2  ON c1.city = c2.city AND    c1.customername <> c2.customerName ORDER BY c1.city

mysql self join customers same city

We joined the customers table to itself with the following join conditions:

  • c1.city = c2.city to make sure that both customers have the same city

  • c.customerName <> c2.customerName to ensure that we don’t get the same customer.

In this tutorial, we have introduced you to MySQL self join that allows you to join a table to itself by using INNER JOIN or LEFT JOIN clauses.

Related Tutorials

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

點擊查看更多內容
TA 點贊

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

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

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

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消