I have two tables cities (as C) and routes (as R)
C:
x
____ ______
| id | city |
|____|______|
| 1 | A |
| 2 | B |
|____|______|
R:
____ ______ ____ __________
| id | from | to | distance |
|____|______|____|__________|
| 1 | 1 | 2 | 100 |
| 2 | 2 | 1 | 100 |
|____|______|____|__________|
(my expectation): I want to combine and join my tables to the following form:
____ ______ ____________ ____ __________ __________
| id | from | fromAsText | to | toAsText | distance |
|____|______|____________|____|__________|__________|
| 1 | 1 | A | 2 | B | 100 |
| 2 | 2 | B | 1 | A | 100 |
|____|______|____________|____|__________|__________|
To add one value isn’t a problem
SELECT
r.*
s.city as fromAsText
FROM
routes as r,
cities as s
WHERE
r.from = s.id
But I haven’t any Ideas how to achieve my goals! Thanks!
Advertisement
Answer
Simply join the city table twice:
select r.id, r.from, c_from.city as from_city, r.to, c_to.city as to_city, r.distance
from routes r
join cities c_from on c_from.id = r.from
join cities c_to on c_to.id = r.to
order by r.id;