PHP计数具有相同ID的值 [英] PHP count values with same id
本文介绍了PHP计数具有相同ID的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当前的项目:我正在尝试创建一个公司所有汽车的概览.
My current project: I'm trying to create an overview of all cars in a company.
我得到以下mysql结果表:
I get the following mysql result table:
|car_id|distance|
| 1 | 33875 |
| 1 | 33027 |
| 1 | 31579 |
| 2 | 125636 |
| 2 | 124937 |
距离"是加满油后的里程数.
The "Distance" is the mileage after a fill up.
我需要什么?
我需要每辆车的总距离.
I need the total distance for each car.
结果应该是这样的:
|car_id|distance|
| 1 | 2296 | (33875 - 31579)
| 2 | 699 | (125636 - 124937)
推荐答案
您可以使用 group by
子句轻松完成,例如:
You can easily do it with group by
clause, e.g.:
SELECT id, (MAX(distance) - MIN(distance)) AS mileage
FROM cars
GROUP BY id;
这是 SQL Fiddle.
这篇关于PHP计数具有相同ID的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文