Aero77 Posted May 3, 2014 Share Posted May 3, 2014 I'm trying to make a diesel consumption scoreboard for the drivers in our company, but not sure how I can sort the drivers by average fuel consumption. I have two database tables, one for the members and one for the fuel data. I'm using this sql query to calculate the average, but I would like to have some more information in the same query and also a way to easily sort the members by their average fuel consumption to complete the scoreboard. SELECT 10 * SUM(liter) / (MAX(km) - MIN(km)) as AvgFuel FROM diesel WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']."; In the diesel table there are information like km, liters, dates, locations etc. How can I extend this query to have more information instead of having multiple queries ? (I'm a newbie) Thanks for reading! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2014 Share Posted May 3, 2014 to sort add ORDER BY AvgFuel to the end of the query To pull in data from another table, use a JOIN http://www.phpfreaks.com/tutorial/data-joins-unions Quote Link to comment Share on other sites More sharing options...
Aero77 Posted May 9, 2014 Author Share Posted May 9, 2014 I've got this mysql query to calculate the average use of fuel for a member, but how can I get more info out from the database in the same query, such as username, kilometers, liters, dates etc ? $result = mysqli_query($con,"SELECT 10 * SUM(liter) / (MAX(km) - MIN(km)) as AvgFuel FROM diesel WHERE userid = ".$_GET['uid']); Quote Link to comment Share on other sites More sharing options...
Barand Posted May 9, 2014 Share Posted May 9, 2014 How many answers do you you want to this question? http://forums.phpfreaks.com/topic/288219-mysql-query-question/?do=findComment&comment=1478080 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.