SQL 查询检索以下条件。有两列,一列以 user_id 作为主键,另一列作为经理键。我想检索查询,以便它显示经理密钥以及用户 ID 作为经理 1、经理 2
尝试一下:您可以使用自连接来实现此目的
SELECT e1.name, e1.managerid, e2.name, e2.user_id
FROM Employee e1
INNER JOIN Employee e2
ON e1.managerid= e2.user_id
尝试一下,您可以使用相同的方法来查找层次结构:
SELECT e1.name AS EmployeeName
,e1.user_id AS USERID
,e2.name AS ManagerName1
,e2.user_id AS ManagerID1
,e3.name AS ManagerName2
,e3.user_id AS ManagerID2
FROM Employee e1
LEFT JOIN Employee e2
ON e1.managerid = e2.user_id
LEFT JOIN Employee e3
ON e2.managerid = e3.user_id