Home > Database > Mysql Tutorial > mysql self-connection query example analysis

mysql self-connection query example analysis

王林
Release: 2023-05-26 21:37:04
forward
1762 people have browsed it

1. Self-join query is a special multi-table join query, because the tables of the two related queries are the same table. They are virtualized into two tables by taking aliases and then connected.

2. A self-join query is to connect itself to itself. Give a table two different aliases, and then attach the connection conditions.

Example

#这些数据全部在员工表中
#把t_employee表,即当做员工表,又当做领导表
#领导表是虚拟的概念,我们可以通过取别名的方式虚拟
SELECT employee.id "员工的编号",emp.ename "员工的姓名" ,emp.salary "员工的薪资",
manager.id "领导的编号" ,manager.ename "领导的姓名",manager.salary "领导的薪资"
FROM emp employee INNER JOIN emp manager
#emp employee:employee.,表示的是员工表的
#emp manager:如果用manager.,表示的是领导表的
ON employee.mgr = manager.id  # 员工的mgr指向上级的id
 
#表的别名不要加"",给列取别名,可以用"",列的别名不使用""也可以,但是要避免包含空格等特殊符号。
Copy after login

The above is the detailed content of mysql self-connection query example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template