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 #表的别名不要加"",给列取别名,可以用"",列的别名不使用""也可以,但是要避免包含空格等特殊符号。
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!