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”.
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