Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:38:15+00:00 2026-05-28T16:38:15+00:00

I am writing a php script to determine the fuel usage of trucks. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:38:15+00:00Added an answer on May 28, 2026 at 4:38 pm

    Add an ORDER BY to your SQL select statement

    ORDER BY odometer ASCENDING
    

    EDIT

    OK! I think I understand your problem now.

    SELECT t1.truck_id,
           t1.odometer AS km1,  
           MIN(t2.odometer) AS km2
      FROM fueldispensed AS t1, 
           fueldispensed AS t2 
     WHERE t2.truck_id = t1.truck_id
       AND T2.odometer > t1.odometer
     ORDER BY t1.truck_id,
              t1.odometer 
     GROUP BY t1.truck_id,
              t1.odometer 
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a php script to compare the similarity of 2 strings. This works
I'm writing a PHP script that does a lot of repetitive work, and when
I'm writing a PHP script that downloads a series of generated files (using wget
I'm writing a php script to export MySQL database rows into a .txt file
I'm writing a PHP script that builds an SQL query by concatenating the string
I was thinking of writing a PHP script that would analyse a CMS'd page's
I am writing a PHP script that will send via a cron an email
I'm writing a php script that takes some c code and tests it against
I'm currently writing a PHP script that 3rd party clients will be able to
Im writing a php script that is used to update a database but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.