我目前正在尝试在同一张 table 上加入两次。
我有一个类似于下表的表格:
旅行:
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'
在这一点上,我很确定我对某些事情的理解不正确,并希望得到任何帮助。
最佳答案
你已经为你的表设置了别名,但是没有在连接子句中使用别名,所以 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;
关于MySQL 加入 'on clause' 中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297866/