我有一个包含批号和距离的查询。它有多个批号和不同的距离。
MySql查询:
SELECT SQL_CALC_FOUND_ROWS claim_trips.distance as total_distance, actions.batch_no
FROM `actions`
JOIN claim_trips ON actions.batch_no=claim_trips.batch_no
WHERE actions.stage = 1 AND actions.`status` = 0
ORDER BY `batch_no` ASC
这是结果
如上面的屏幕截图所示,有2个不同的批号。我怎么能得到两个批号下的总距离?与MySql Query有什么关系?
答案 0 :(得分:1)
只是SUM distanses和GROUP BY batch_no:
SELECT SQL_CALC_FOUND_ROWS
SUM(claim_trips.distance) AS total_distance,
actions.batch_no
FROM
`actions`
JOIN claim_trips ON actions.batch_no = claim_trips.batch_no
WHERE
actions.stage = 1
AND actions.`status` = 0
GROUP BY
actions.batch_no
ORDER BY
`batch_no` ASC