SQL在不同列上多次连接表

时间:2014-09-09 11:59:04

标签: mysql sql join left-join inner-join

我有两张桌子:

personal
____________
| id | name |
| 15 | Mike |
| 23 | Rich |
| 35 | Hugo |

events
___________________________
| id | driver | translator |
| 22 | 15     | 23         |
| 23 | 35     | 35         | 

有没有办法加入这两个表来获得像

这样的东西
events

___________________________________
| id | driverName | translatorName |
| 22 | Mike       | Rich           |
| 23 | Hugo       | Hugo           |

THX

2 个答案:

答案 0 :(得分:1)

你可以试试这个

SELECT 
  e.id
  ,p1.name driverName
  ,p2.name translatorName 
  FROM `events` e
  JOIN `personal` p1
    ON p1.id=e.driver
  JOIN `personal` p2
    ON p2.id=e.translator 

答案 1 :(得分:1)

这将有效

select e.id, (select name from Personal where id = e.driver) as DiverName, 
       p.name as TranslatorName
from Personal p
inner join [events] e on p.id = e.translator