MySQL在' on子句'中加入未知列

时间:2014-12-04 15:33:45

标签: mysql sql join

我目前正在尝试两次加入同一张桌子。

我有一张类似于下面的表格:

跳闸:

arrival_loc_code
leaving_loc_code

还有另一个表格,其中包含这些位置的值:

位置:

location_code (pk)
location

我试图根据行程中的位置代码获取到达和离开的位置。我目前的尝试导致:

select leavingloc.location,arrivalloc.location from trip 
    join locations as leavingloc on trip.departureloccode = locations.locationcode 
    join locations as arrivalloc on trip.arrivalloccode = locations.locationcode;

导致我:

ERROR 1054 (42S22): Unknown column 'locations.locationcode' in 'on clause'

在这一点上,我非常确定我有一些错误的理解,并希望得到任何帮助。

3 个答案:

答案 0 :(得分:3)

你给表别名但后来没有使用join子句中的别名,所以mysql变得混乱:

select leavingloc.location, arrivalloc.location from trip 
    join locations as leavingloc on trip.departureloccode = leavingloc.locationcode 
    join locations as arrivalloc on trip.arrivalloccode = arrivalloc.locationcode;

答案 1 :(得分:0)

这是因为您的alisas名称与您使用的名称不同。

更改

   join locations as leavingloc on trip.departureloccode = locations.locationcode 
    join locations as arrivalloc on trip.arrivalloccode = locations.locationcode;

 join locations as leavingloc on trip.departureloccode = leavingloc.locationcode 
    join locations as arrivalloc on trip.arrivalloccode = arrivalloc.locationcode;

答案 2 :(得分:0)

在您的on子句中,请参阅'位置'你给他们的别名(leavingloc或arrivalloc)而不是'位置'

的表格