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

我有一个表格,看起来类似于下面:

行程:

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'


在这一点上,我很确定我理解不正确,希望能对您有所帮助。

最佳答案

您已经为表加上了别名,但是没有在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;

08-06 23:27