我有一张表存储两个相邻火车站之间的时间。
+------+------+------+
| s1id | s2id | tbtw |
+------+------+------+
| 234 | 235 | 20 |
| 235 | 133 | 8 |
| 133 | 108 | 15 |
| 234 | 236 | 10 |
| 108 | 500 | 2 |
| 235 | 108 | 21 |
+------+------+------+
我想找到任意两点之间的时间,如果它们可以连接,就像找到从 234 站到 500 站的时间(234->235->108->500)一样。我知道这就像一个图表。我试图找到 t2.s1id = t1.s2id 的累积距离,如下所示
select t1.* SUM(t2.tbtw) as sum
from t t1
join t t2 on t1.s2id = t2.s1id
group by t1.id, t1.tbtw
但这并没有给我累积时间或者没有正确链接到节点
select t1.*
(select sum(tbtw)
from t t2
where t2.s1id = t1.s2.id
) as sum
from t t1;
我可以很容易地用编程语言做到这一点,但在 sql 中,它真的很困惑。我必须使用程序吗?我不能用简单的sql语句做吗?在简单的sql语句中首选该解决方案,但其他解决方案也可以。请帮助我。
最佳答案
参见文章:http://hansolav.net/sql/graphs.html
我觉得很有帮助
关于mysql - sql - 查找两个图点之间的成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26396996/