hzk20220818

mysql 连接-自连接

自连接

表连接自己,通过某个条件

Id | Name  | Salary | ManagerId 
----+-------+--------+-----------
 1  | Joe   | 70000  | 3         
 2  | Henry | 80000  | 4         
 3  | Sam   | 60000  | NULL      
 4  | Max   | 90000  | NULL

例子:查找收入超过各自经理的员工的姓名

SELECT w1.Id
FROM weather w1
JOIN weather w2
ON DATEDIFF(w1.RecordDate,w2.RecordDate)=1
WHERE w1.Temperature>w2.Temperature

注意:自连接一定要给两张一样的表起不一样的别名

用法:

from table1 as t1,table as t2
from table1 as t1 join table as t2

posted on 2022-10-09 10:38  hzk-shzdx-university  阅读(37)  评论(0编辑  收藏  举报

导航