tab_city
id_city city
1 Paris
2 Texas
3 Toquio
tab_control
id_control origin destination
1 1 3
对于以前的表格,我需要从tab_control获取数据,并从tab_city获取相应的城市。
如果仅是起源,则非常简单:
SELECT origin.tco, city.tci
FROM tab_control tco
LEFT JOIN tab_city tci ON tco.origin = tci.id_city
返回
origin = 1
city = Paris
但是,我需要得到
origin = 1
city = Paris
destination = 3
city_destination?? = Toquio
答案 0 :(得分:0)
您需要另一个JOIN
作为目的地
SELECT tco.origin, tci.city, tco.destination, d.city
FROM tab_control tco
LEFT JOIN tab_city tci ON tco.origin = tci.id_city
LEFT JOIN tab_city d ON tco.destination = d.id_city