简体   繁体   English

查询以使用外键获取行值(first_name 和 last_name)

[英]Query to fetch row values (first_name & last_name) using foreign key

New to SQL and MySQL. SQL 和 MySQL 的新手。

In the below table, I've a table of employees where emp_id is employee's ID & super_id is supervisor's employee ID.在下表中,我有一张员工表,其中 emp_id 是员工的 ID,super_id 是主管的员工 ID。 Here super_id is assigned as foreign key of employee table itself.这里 super_id 被分配为员工表本身的外键。

员工表

Now I need to fetch a table with three columns: first_name (supervisor), last_name (supervisor) and Count of employees who are supervised by this employee.现在我需要获取一个包含三列的表:first_name(主管)、last_name(主管)和受该员工监督的员工人数。

For Example:例如:

David     Wallace   3
Michael   Scott     3
Josh      Porter    2

Thanks in advance提前致谢

This is a 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name 如何编写查询以获取订单包含超过 2 件商品的客户的名字和姓氏? - How do I write a query to get the first_name and last_name of customers having orders containing more than 2 items? 更新名称后,数据库中的“ first_name”显示为0,last_name显示为“ Arora” - into my database `first_name` is showing as 0 and last_name is showing as “Arora” after updating name 获取所有具有相同first_name的用户last_name - Get all users last_name which have same first_name 从指向同一个表的两个关系中过滤 first_name 和 last_name - filter first_name and last_name from two relation pointing to same table 将 first_name 和 last_name 列中的名称截断为 1 个字符的最有效方法是什么? - What's the most efficient way of truncating the names in a first_name and last_name column to 1 character? 搜索带有尾随通配符的first_name和last_name的最佳索引? - Best index for searching both first_name and last_name with trailing wildcards? PHP-如何在登录后获取正确的名字和姓氏? - PHP - How to get correct first_name and last_name after login? 我正在向服务器发出 Ajax post 请求,所有数据都很好,但我的 first_name 和 last_name 被更新为“未定义” - I am doing a Ajax post request to the server all the data is fine but my first_name and last_name are being updated as "undefined" 查询员工的名字和经理的名字的语法是什么? - What is a syntax to query a first_name of employees and first_name of their managers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM