同一张表,1字段转2字段查询
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要嵌套查询,只需使用标准联接:
每一行将包含
employee
的所有字段(如果有多个关联的经理,则可能为一名员工包含几行)及其相应经理的所有字段(或NULL)。
No nested queries required, just use standard joins:
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 (orNULL
s if employee has no manager).您可以使用一张表来完成此操作:
ManagerId
指向同一表中经理的条目。 CEO 的 ManagerId 将为null
。示例表定义:使用一些示例数据:
要查找第二个 Grunt 经理的姓名,您可以像这样查询:
You can do that with one table:
ManagerId
points to the manager's entry in the same table. The CEO will have a ManagerId ofnull
. An example table definition:With some example data:
To find the name of the second Grunt's manager, you could query like: