我有两个桌子。
tbl_cities:
Id, Name
---------
1 | Paris
2 | London
3 | Amsterdam
4 | Mumbai
tbl_journeys:
from_id, to_id, Journey_date
---------------------------------
1 | 2 | 1 Mar 2019
2 | 1 | 1 Mar 2019
3 | 1 | 3 Mar 2019
产生这样的输出的SQL是什么:
City_From_Name, City_To,Name, Journey_Date
------------------------------------------------
Paris | London | 1 Mar 2019
London | Paris | 1 Mar 2019
Amsterdam | London | 3 Mar 2019
答案 0 :(得分:1)
与tbl_cities两次
select b.name as City_From_Name,b1.name as City_To,Name,journey_date
from
tbl_journeys a inner join tbl_cities b on a.from_id=b.id
inner join tbl_cities b1 on a.to_id=b1.id
答案 1 :(得分:1)
请尝试这个。
Select B.Name As City_From_Name, C.Name As City_To_Name, A.Journey_Date from tbl_journeys A
LEFT JOIN tbl_cities B
ON B.id = A.From_id
LEFT JOIN tbl_cities C
ON C.id = A.To_id
答案 2 :(得分:1)
SELECT c1.Name City_From_Name , c2.Name City_To,Name, j.Journey_date Journey_Date
FROM tbl_journeys j
INNER JOIN tbl_cities c1 ON c1.Id = j.from_id
INNER JOIN tbl_cities c2 ON c2.Id = j.to_id
答案 3 :(得分:0)
尝试一下
从tbl_cities,tbl_journeys中选择tbl_cities.Name作为“ City_from_Name”,tbl_journeys.to_Id作为“ City_to_Name”,tbl_journeys.Journeys.date,其中tbl_cities.Id = tbl_journeys.from_id;