mysql 子连接_MySQL 子连接示例

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.

article-87233-1.html

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

article-87233-1.html

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

article-87233-1.html

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

article-87233-1.html

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值