如何获取数据库表行的总和值以及ID

时间:2016-03-25 06:46:19

标签: php mysql

我有一个包含批号和距离的查询。它有多个批号和不同的距离。

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

这是结果

enter image description here

如上面的屏幕截图所示,有2个不同的批号。我怎么能得到两个批号下的总距离?与MySql Query有什么关系?

1 个答案:

答案 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