简体   繁体   English

SQLite3,将多行合二为一

[英]SQLite3, Combine multiple rows in one

I have two tables cities (as C) and routes (as R)我有两个表城市(如 C)和路线(如 R)

C: C:

 ____ ______
| 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!谢谢!

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM