【发布时间】:2012-11-03 19:02:51
【问题描述】:
我有一个名为 Tracking 的表格:
ID_path -- step_start -- step_end -- time
1 A B 10
1 B C 20
1 C D 30
2 A C 100
3 D C 20
3 A N 300
我正在 Mysql 中寻找可以提取路径 A-C 的累积值的单个查询,因此它本身和所有步骤都进入:[A-C] + [A-B] + [B-C]。
选择 sum(time) from tracking where (step_start = 'A' or step_end = 'C') and ID_path = ID_path
我认为这个查询考虑到 step 的值不能同时在 start 和 end 但我不知道如何表达最后一个条件 (ID_path = ID_path) 以便只提取具有相同的记录id_path。 有具体的 SQL 函数吗?
感谢任何帮助/mysql 编码指南/建议
【问题讨论】:
-
@SashiKant 这和等级有什么关系?
-
@Barmar:如果你回答这个问题,要求几乎相同
-
这个问题与求和无关,它是关于将父子表转换为 PHP 嵌套数组。
-
@SashiKant 看来你的推荐在stackoverflow.com/questions/13392648/…会更好
标签: mysql sql select conditional-statements