同一张表,1字段转2字段查询

发布于 2024-08-27 11:15:11 字数 166 浏览 7 评论 0原文

我有 2 个表:第一个表保存员工(任何职位的员工),第二个表保存经理员工关系和 ID 号。

我想编写一个查询,例如

1st field: name(employee), 
2nd field: name(manager)

我该怎么做?

I have 2 tables: 1st holds employees (of ones in any position) and the 2nd holds manager employee relations with id numbers.

I want to write a query like

1st field: name(employee), 
2nd field: name(manager)

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-09-03 11:15:11

不需要嵌套查询,只需使用标准联接:

select e.*, m.*
from
  employee e
    left join employee_managers em
      on e.id = em.emp_id
    left join employee m
      on m.id = em.man_id

每一行将包含 employee 的所有字段(如果有多个关联的经理,则可能为一名员工包含几行)及其相应经理的所有字段(或NULL)。

No nested queries required, just use standard joins:

select e.*, m.*
from
  employee e
    left join employee_managers em
      on e.id = em.emp_id
    left join employee m
      on m.id = em.man_id

Each row will contain all fields of employee (possibly several rows for one employee if it has several associated managers) and all fields of his corresponding manager (or NULLs if employee has no manager).

ぽ尐不点ル 2024-09-03 11:15:11

您可以使用一张表来完成此操作:

 Employee
 --------
 EmployeeId int
 Name varchar(50)
 ManagerId int

ManagerId 指向同一表中经理的条目。 CEO 的 ManagerId 将为 null。示例表定义:

create table Employees (
  EmployeeId int auto_increment primary key
, Name varchar(50)
, ManagerId int
, foreign key (ManagerId) references Employees(EmployeeId)
);

使用一些示例数据:

insert into Employees (Name) select 'The Chief';
insert into Employees (Name, ManagerId) select 'Grunt 1', 
    (select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Grunt 2', 
    (select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Secretary', 
    (select EmployeeId from Employees where Name = 'The Chief');

要查找第二个 Grunt 经理的姓名,您可以像这样查询:

select mgr.Name
from Employees mgr
inner join Employees grunt
on grunt.managerid = mgr.employeeid
where grunt.name = 'Grunt 2';

You can do that with one table:

 Employee
 --------
 EmployeeId int
 Name varchar(50)
 ManagerId int

ManagerId points to the manager's entry in the same table. The CEO will have a ManagerId of null. An example table definition:

create table Employees (
  EmployeeId int auto_increment primary key
, Name varchar(50)
, ManagerId int
, foreign key (ManagerId) references Employees(EmployeeId)
);

With some example data:

insert into Employees (Name) select 'The Chief';
insert into Employees (Name, ManagerId) select 'Grunt 1', 
    (select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Grunt 2', 
    (select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Secretary', 
    (select EmployeeId from Employees where Name = 'The Chief');

To find the name of the second Grunt's manager, you could query like:

select mgr.Name
from Employees mgr
inner join Employees grunt
on grunt.managerid = mgr.employeeid
where grunt.name = 'Grunt 2';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文