Origin-Destination Table连接

时间:2016-08-03 13:24:26

标签: sql

如何join以下表格?或者这可能吗?

origindestination是fk。

**tbleroute**
ID      ORIGIN        DESTINATION
1         1                 3
2         1                 2
3         3                 4

**tblelocation**
ID       Name
1        Bus Station
2        Market
3        Bus Station 2
4        School

1 个答案:

答案 0 :(得分:1)

对于明显的结果,您需要两个连接。这看起来像:

select r.*, lo.name as origin_name, ld.name as destination_name
from tblrouter r join
     tbllocation lo
     on r.origin = lo.id join
     tbllocation ld
     on r.destination = ld.id;

这种情况是from子句中需要表别名来区分表。