查询以使用外键获取行值 (first_name & last_name)
Query to fetch row values (first_name & last_name) using foreign key
SQL 和 MySQL 的新手。
在下面的 table 中,我有 table 名员工,其中 emp_id 是员工的 ID,super_id 是主管的员工 ID。这里 super_id 被指定为员工 table 本身的外键。
现在我需要获取包含三列的 table:first_name(主管)、last_name(主管)和受此员工监督的员工人数。
例如:
David Wallace 3
Michael Scott 3
Josh Porter 2
提前致谢
这是一个self-join:
select tsuper.first_name, tsuper.last_name, count(*)
from t join
t tsuper
on t.super_id = tsuper.emp_id
group by tsuper.first_name, tsuper.last_name;
SQL 和 MySQL 的新手。
在下面的 table 中,我有 table 名员工,其中 emp_id 是员工的 ID,super_id 是主管的员工 ID。这里 super_id 被指定为员工 table 本身的外键。
现在我需要获取包含三列的 table:first_name(主管)、last_name(主管)和受此员工监督的员工人数。
例如:
David Wallace 3
Michael Scott 3
Josh Porter 2
提前致谢
这是一个self-join:
select tsuper.first_name, tsuper.last_name, count(*)
from t join
t tsuper
on t.super_id = tsuper.emp_id
group by tsuper.first_name, tsuper.last_name;