MySQL的子连接_MySQL 子连接示例

本文介绍了MySQL中的自连接操作,通过使用INNER JOIN或LEFT JOIN将表与自身进行连接。示例中展示了如何通过自连接获取组织结构,包括员工的上下级关系,并解释了如何通过LEFT JOIN包含没有直接上级的顶级经理。此外,还提供了一个例子展示如何找出位于同一城市的客户。
摘要由CSDN通过智能技术生成

Summary:in this tutorial, you will learn how to useMySQL self jointhat joins a table to itself using join statement.

In the previous tutorial, you have learned how to join a table to the other tables usingINNER JOIN,LEFT JOINorRIGHT JOINstatement. 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 atable aliasto help MySQLdistinguish the left table from the right table of the same table.

MySQL Self Join Examples

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

In theemployeestable, we store not only employees data but also organization structure data. Thereportstocolumn is used to determine the manager ID of an employee.

portal.php?mod=view&aid=87233

In order to get the whole organization structure, we can join theemployeestable to itself using theemployeeNumberandreportsTocolumns. Theemployeestable has two roles: one isManagerand the other isDirect Report.

SELECTCONCAT(m.lastname,',',m.firstname)AS'Manager',CONCAT(e.lastname,',',e.firstname)AS'Directreport'FROMemployeeseINNERJOINemployeesmONm.employeeNumber=e.reportstoORDERBYmanager

portal.php?mod=view&aid=87233

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 theINNER JOINclause. The top manager is the employee who does not have manager or his manager no. isNULL.

Let’s change theINNER JOINclause to theLEFT JOINclause in the query above to include the top manager. We also need to use theIFNULLfunction to display the top manager if the manger’s name isNULL.

SELECTIFNULL(CONCAT(m.lastname,',',m.firstname),'TopManager')AS'Manager',CONCAT(e.lastname,',',e.firstname)AS'Directreport'FROMemployeeseLEFTJOINemployeesmONm.employeeNumber=e.reportstoORDERBYmanagerDESC

portal.php?mod=view&aid=87233

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

SELECTc1.city,c1.customerName,c2.customerNameFROMcustomersc1INNERJOINcustomersc2ONc1.city=c2.cityANDc1.customername<>c2.customerNameORDERBYc1.city

portal.php?mod=view&aid=87233

We joined thecustomerstable to itself with the following join conditions:c1.city = c2.cityto make sure that both customers have the same city

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

In this tutorial, we have introduced you to MySQLselfjoin that allows you to join a table to itself by usingINNER JOINorLEFT JOINclauses.

Related TutorialsMySQL INNER JOIN

MySQL LEFT JOIN

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值