php mysql 计算连续行
我正在编写一个 php 脚本来确定卡车的燃油使用量。我为此使用 mysql 数据库表。
卡车可以在多个地点获取燃料,例如 A、B、C、D 地点。 卡车从最近的这些地点之一获取燃料。而卡车每次加油时,负责人都会输入“加油量”和“里程表”的数值进行编程。
sequence_id locations fuelDispensed odometer
1 C 700 8100
2 A 400 9700
3 B 500 15500
4 C 600 17950
等等。
有了来自数据库的这些信息,只需使用“sequence_id”计算连续行之间的“里程表”差异,就可以轻松找到卡车从一个位置到另一个位置行驶了多少公里或英里。
问题是:人们可能会忘记或无法将值输入程序并稍后再执行。数据变成这样:
sequence_id locations fuelDispensed odometer
1 C 700 8100
2 B 500 15500
3 C 600 17950
4 A 400 9700
在这种情况下,不可能根据sequence_id在连续行之间进行计算。也许,通过对里程表值升序排序,然后在行之间进行连续计算似乎是合乎逻辑的,但我不知道如何做到这一点。
编辑:我的查询是这样的:
SELECT
t1.odometer AS km1,
t2.odometer AS km2,
FROM fueldispensed AS t2, fueldispensed AS t1
WHERE (t1.sequence_id+1= t2.sequence_id) AND (t1.truck_id='$truckid') AND (t2.truck_id='$truckid') ORDER BY t1.sequence_id";
向此查询添加 ORDER BY 没有任何效果,因为我在“sequence_id”上获得了继承。
I am writing a php script to determine the fuel usage of trucks. I use mysql db table for this.
There are several locations that a truck can get fuel, say A, B, C, D locations.
The truck gets fuel from one of these locations which is the closest. And every time the truck gets fuel, the person responsible will enter "the amount of the fuel" and value of "odometer" to program.
sequence_id locations fuelDispensed odometer
1 C 700 8100
2 A 400 9700
3 B 500 15500
4 C 600 17950
and so on.
With this info from db, It is easy to find how many KMs or miles the truck travelled from a location to another just by calculating "odometer" difference between successive rows by using "sequence_id".
The problem is: People may forget or not be able to enter the values to the program and do it later. the data becomes like this:
sequence_id locations fuelDispensed odometer
1 C 700 8100
2 B 500 15500
3 C 600 17950
4 A 400 9700
In this case, it is not possible to calculate between successive rows based on sequence_id. Maybe, by sorting odometer values ascending and then doing successive calculation between rows seems logical but I could not find out how I can do this.
Edit: My query is something like this:
SELECT
t1.odometer AS km1,
t2.odometer AS km2,
FROM fueldispensed AS t2, fueldispensed AS t1
WHERE (t1.sequence_id+1= t2.sequence_id) AND (t1.truck_id='$truckid') AND (t2.truck_id='$truckid') ORDER BY t1.sequence_id";
adding ORDER BY to this query has no effect since I get the succession on "sequence_id".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 ORDER BY 添加到 SQL select 语句
编辑
OK!我想我现在明白你的问题了。
应该给你一些有用的东西,尽管效率不高,将
你的 Truck_id 选择适当地编辑到查询中
Add an ORDER BY to your SQL select statement
EDIT
OK! I think I understand your problem now.
Should give you something that will work, though not as efficient as it could be
Edit your truck_id selection into the query as appropriate