我有下表:
+------+-----------+----------+
| Item | ProcessID | PersonID |
+------+-----------+----------+
| 111 | 33 | 1234567 |
+------+----+------------+
| Item | ID | DelegateID |
+------+----+------------+
| 111 | 1 | 4567894 |
+----------+------+
| PersonID | Name |
+----------+------+
| 1234567 | Jhon |
+----------+------+
| 4567894 | Larry|
我想这样加入他们
+-----------+--------+----------+
| ProcessID | Person | Delegate |
+-----------+--------+----------+
| 33 | Jhon | Larry |
但是仅仅做一个简单的连接并不能使我到达那里。
答案 0 :(得分:1)
SELECT Table1.processid, Persons.name,
(select name from Persons where personid =Table2.delegatepersonid) as delegate
FROM Persons
INNER JOIN Table1 ON Persons.personid = Table1.personid
INNER JOINTable2 ON Table1.item= Table2.item
答案 1 :(得分:0)
我认为这需要人员表的别名,因此可以在两个联接中使用它:personid和委托人id:
select P.*, T1.*, T2.*
from persons P
inner join table_1 T1 on P.personid = T1.personid
inner join table_2 T2 on T1.item = T2.item
inner join persons P_del on T2.delegatepersonid = P_del.personid ;
答案 2 :(得分:0)
由于您要查找两个不同的Personid值,因此必须两次加入Persons
。
SELECT
one.processid
, pone.name
, ptwo.name
FROM table1 AS one
INNER JOIN table2 AS two
ON one.item = two.item
INNER JOIN persons as pone
ON pone.personid = one.personid
INNER JOIN persons as ptwo
ON ptwo.personid = two.delegatepersonid
我相信周星驰的解决方案也可能有效。