首页 > 解决方案 > 如果 Id,如何获取名称

问题描述

在此处输入图像描述

我想获取所有数据,但不是主管和团队负责人中的 id 我想获取他们的姓名或电子邮件 ID

前任

如果我获取 id 8 数据,它应该是这样的:

id -> 8
user_name -> ag2
password -> 121212
email -> ag2@ag.com
mobile->21341234
superwisor -> kk(user_name of Id 1)
teamleader -> tl2(user_name of id 4)

标签: mysqlsql

解决方案


您需要self join按如下方式使用:

Select e.id, e.user_name, e.email, e.password, e.mobile, 
       s.user_name as superwisor, t.user_name as team_leader
From your_table e
Left join your_table s on s.id = e.superwisor
Left join your_table t on t.id = e.teamleader;

推荐阅读