简体   繁体   English

如何访问mysql中另一个表的两个不同列对应的表的一列?

[英]How to access one column of a table corresponding to two different columns of another table in mysql?

I want to fetch column EmployeeName from Table A associated with toEmployeeId and fromEmployeeId in Table B using one query only.我想仅使用一个查询从与表 B 中的 toEmployeeId 和 fromEmployeeId 关联的表 A 中获取列 EmployeeName。

Here are my tables这是我的桌子

Table A
|---------------------|------------------|
|    Employeeid       |  EmployeeName    |
|---------------------|------------------|
|          E1         |       ABC1       |
|---------------------|------------------|
|          E2         |       ABC2       |
|---------------------|------------------|
|          E3         |       ABC3       |
|---------------------|------------------|

Table B
|---------------------|------------------|
|    toEmployeeid     |  fromEmployeeid  |
|---------------------|------------------|
|          E1         |       E3         |
|---------------------|------------------|
|          E2         |       E1         |
|---------------------|------------------|
|          E3         |       E1         |
|---------------------|------------------|

Here is the query I am trying to run but it is not working这是我试图运行的查询,但它不起作用

select A.Employeeid, A.employeename,
      (select employeename
       from A,
            B
       where A.employeeid = B.fromemployeeid)
from A,
     B
where B.toEmployeeid = A.id;

You should use the same table A two time with proper alias A1 e A2您应该使用正确的别名 A1 e A2 两次使用同一个表 A

select B.toEmployeeid
    , A1.EmployeeName
    ,  B.fromEmployeeid
    , A2.EmployeeName
from B
INNER JOIN  A A1  ON A1.Employeeid = B.toEmployeeid
INNER JOIN  A A2  ON A2.Employeeid = B.fromEmployeeid

This is how I got it to work.这就是我让它工作的方式。 For anyone looking for an answer.对于任何寻找答案的人。

select At.Employeeid, At.employeename,
      (select employeename from A as At
       where At.employeeid = Bt.fromemployeeid) as fromEmpId
from A as At,
     B as Bt
where Bt.toEmployeeid = At.Employeeid;

暂无
暂无

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

相关问题 MySQL将两个表列复制到具有不同列名的另一个表 - MySQL copy two table columns to another table with different column names mysql-表的两列和另一列的计数 - mysql - count of two columns of a table and one column from another 将一列从一个表复制到mysql中的另一个表(两个不同的数据库) - Copy a column from one table to another in mysql (two different databases) MySQL查询以查找一个表中一列的值是否在另一表中两列的两个值之间 - MySQL query to find if a value of one column in one table is between two values in two columns on another table 如何查询一个表的同一列中的两个不同对象,这些对象与另一个表中的不同列相关? - How can i make a query of two different objects in the same column of one table, relationed to different columns in another table? 如何将两个mysql表列添加到datagridview中的一列? - How to add two mysql table columns to one column in datagridview? 将一列复制到另一张表中的mysql - Copy one column to another in different table mysql mysql:如何使用另一个表的两个不同值从一个表中选择两个不同的列值 - mysql :How to select two different column values from a single table using two different values of another table MySQL-连接两个表,表1中不同的列在表2中的同一列上 - MySQL - Join two tables, different columns in table 1 on the same column in table 2 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM