简体   繁体   English

根据同一表中的另一个字段从一个字段检索值

[英]retrieve value from one field based on other field in same table

I have two tables based on which i need a final table which uses field of first table to fetch other field value from same table. 我有两个基于的表,我需要一个最终表,该最终表使用第一个表的字段从同一表中获取其他字段值。 table1-emp_details table1-emp_details

emp_no emp_no_manager  dept_no salary    emp_name
1               4      1        10000    emp1
2               5      1        14000    emp2
3               4      1        23000    emp3
4               5      1        40000    emp4     
5               6      2        60000    emp5
6                      3        80000    emp6

table2-dept_details table2-dept_details

dept_no dept_name
1       it services
2       consulting services
3       procurement
4       finance
5       hr

expected result 预期结果

emp_no emp_name manager_name dept_name           salary
1      emp1      emp4        it services         10000
2      emp2      emp5        it  services        14000
3      emp3      emp4        it services         23000
4      emp4      emp5        it services         40000
5      emp5      emp6        consulting services 60000
6      emp6                  procurement         80000

This are basic JOIN operations. 这是基本的JOIN操作。 You should really check some SQL tutorial first. 您确实应该先检查一些SQL教程。

SELECT 
   e.emp_no
  ,e.emp_name
  ,m.emp_name AS manager_name
  ,d.dept_name
  ,e.salary
FROM emp_details e
LEFT JOIN emp_details m ON e.emp_no_manager = m.emp_no
LEFT JOIN dept_details d ON e.dept_no = d.dept_no

SQL Fiddle DEMO SQL小提琴演示

Presuming you are only interested in an employees direct/line manager then i think this will do 假设您只对员工的直接/直线经理感兴趣,那么我认为这会做

SELECT
    ed1.emp_no,
    ed1.emp_name,
    ed2.emp_name,
    dd.dept_name,
    ed1.salary
FROM emp_details ed1
JOIN emp_details ed2 ON ed2.emp_no = ed1.emp_no_manager
JOIN dept_details dd ON dd.dept_no = ed1.dept_no

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

相关问题 如何从ms-access检索数据,其中一个字段位于另一张表中,而另一个字段位于同一日期? - How to retrieve data from ms-access where one field is in another table and are on the same date in the other table? SQL Server-从同一视图表中的其他字段创建值一个字段 - SQL Server - create value one field from other field in same View table 一个字段中的值作为从同一表中查找的值 - Value in one field as lookup from same table MySQL 查询以根据同一表中其他字段的值更新表中的字段 - MySQL query to update field in a table based on value of other field in same table 根据同一表的其他字段更新字段 - Update field based on other fields of the same table 从同一表的其他列更新mysql列字段值 - Update mysql columns field value from other columns of the same table 从表中选择一个字段具有相同值的行 - Selecting rows from a table that have the same value for one field MySQL如何将数据从一个字段复制到同一表的另一个字段 - Mysql how to copy data from one field to other field in same table 如何用同一视图表 (SQL) 中另一个字段(不同列)的值替换一个字段中的值? - How to replace a value in one field by a value from another field (different column) within the same view table (SQL)? 根据另一字段的 ID 更新具有相同值的字段 - Updating one field with the same value based on the ID of another field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM