我有两个表的城市(如C)和路线(如R)
C:
____ ______
| id | city |
|____|______|
| 1 | A |
| 2 | B |
|____|______|
R:
____ ______ ____ __________
| id | from | to | distance |
|____|______|____|__________|
| 1 | 1 | 2 | 100 |
| 2 | 2 | 1 | 100 |
|____|______|____|__________|
(我的期望):我想将表合并并联接为以下形式:
____ ______ ____________ ____ __________ __________
| id | from | fromAsText | to | toAsText | distance |
|____|______|____________|____|__________|__________|
| 1 | 1 | A | 2 | B | 100 |
| 2 | 2 | B | 1 | A | 100 |
|____|______|____________|____|__________|__________|
添加一个值不是问题
SELECT
r.*
s.city as fromAsText
FROM
routes as r,
cities as s
WHERE
r.from = s.id
但是我对如何实现目标一无所知!谢谢!
答案 0 :(得分:2)
只需两次加入城市餐桌:
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;