如果 table 没有像 Employee_id 这样的公共数字列或 Mysql 中的员工编号,如何查找员工姓名及其主管姓名

How to find the Employee names and their supervisor names if the table doesn't have common numeric column like Employee_id or employee number in Mysql

如果 table 只有两列包含员工姓名和他们的主管列,并且没有任何其他带有 employee_number 或 employee_id 的数字或数字列,则如何产生结果。我没有逻辑来显示结果。

在 Mysql 中创建 table 的代码:

    CREATE TABLE DATABASE_TABLE 
    (
      Employee_Name nvarchar(255) PRIMARY KEY,
      Supervisor_Name nvarchar(255) NOT NULL
    );
    
    CREATE INDEX ix_database_table_supervisor
        ON DATABASE_TABLE (Supervisor_Name);
    
    
    INSERT INTO DATABASE_TABLE
(Employee_Name, Supervisor_Name) VALUES 
  ('Alice','Dave'), ('Olive','Dave'), ('Barton','Dave')
, ('Almira','Jacob'), ('Charles','Jacob'), ('Davis','Jacob')
, ('Robert','Risha'), ('Peter','Risha'), ('Ethel','Risha')
, ('Isaac','Jospeh'), ('Sophia','Jospeh'), ('Rosa','Jospeh')
, ('Joshua','Dandy'), ('Silas','Dandy'), ('Fred','Dandy')
, ('Frank','Andrew'), ('Howard','Andrew'), ('Ralph','Andrew')
, ('Dennis','Henry'), ('Alex','Henry'), ('Floyd','Henry')
, ('Carlos','Nelson'), ('Homer','Nelson'), ('Harold','Nelson')
, ('Leo','Simon'), ('Warren','Simon'), ('Clifford','Simon')
, ('Martha','Casper'), ('Hazel','Casper'), ('Irene','Casper')
, ('Dave','Betsy'), ('Jacob','Betsy'), ('Risha','David')
, ('Jospeh','David'), ('Dandy','Phillip'), ('Andrew','Phillip')
, ('Henry','Harvey'), ('Nelson','Harvey'), ('Simon','Paul')
, ('Casper','Paul'), ('Betsy','Joe'), ('David','Joe')
, ('Phillip','Joe'), ('Harvey','Joe'), ('Paul','Joe')

它的输出是:

Employee_name   Supervisor_name
Frank           Andrew
Howard          Andrew
Ralph           Andrew
Dave            Betsy
Jacob           Betsy
Hazel           Casper
Irene           Casper
Martha          Casper
Fred            Dandy
Joshua          Dandy
Silas           Dandy
Alice           Dave
Barton          Dave
Olive           Dave
Jospeh          David
Risha           David
Henry           Harvey
Nelson          Harvey
Alex            Henry
Dennis          Henry
Floyd           Henry
Almira          Jacob
Charles         Jacob
Davis           Jacob
Betsy           Joe
David           Joe

....

结果应该在从低到高的层次结构中,如:

Employee_Name   Supervisor_Name  Higher_Supervisor  Next_higher_Supervisor
Frank           Andrew           Phillip            Joe
Howard          Andrew           Phillip            Joe
Ralph           Andrew           Phillip            Joe
Dave            Betsy            Joe                no_supervisor
Jacob           Betsy            Joe                no_supervisor
Hazel           Casper           Paul               Joe
Irene           Casper           Paul               Joe
Martha          Casper           Paul               Joe

For Eg: Frank's supervisor is Andrew, Andrew's supervisor is Phillip, Phillip's supervisor is Joe
For Eg: Dave's supervisor is Betsy, Betsy's supervisor is Joe, and Joe doesn't have any supervisor so no_supervisor should be displayed.
For Eg: Hazel's supervisor is Casper, Casper's supervisor's is Paul, and Paul's Supervisor is Joe should be displayed in the order format

对于这组特定的数据,它可以LEFT JOIN table 本身来获得预期的结果

SELECT a.Employee_Name, a.Supervisor_Name, b.Supervisor_Name, c.Supervisor_Name
FROM DATABASE_TABLE a
LEFT JOIN DATABASE_TABLE b ON a.Supervisor_Name = b.Employee_Name
LEFT JOIN DATABASE_TABLE c ON b.Supervisor_Name = c.Employee_Name

如果层级深度未知,即列数未知,则比较复杂。仍然可以通过使用递归 CTE 来查找深度并生成动态 SQL.