keeps21 Posted May 18, 2009 Share Posted May 18, 2009 I have a table containing player_id, goals and assists. player id | goals | assists| ------------------------- 1 | 10 | 11 2 | 0 | 1 3 | 4 | 4 4 | 2 | 0 ------------------------ What I want to do is pull the data from the database, get the sum of goals and assists as points for each row and order by points descending. I have the following code $sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists FROM ww_players LIMIT 0,9"; $result = mysql_query($sql) or die (mysql_error()); $i=0; while ($row = mysql_fetch_assoc($result)) { $data[] = $row; $i++; } I can add the two values when outputting the data but I want to be able to order by the total before doing this. How do I go about doing it? Quote Link to comment https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/ Share on other sites More sharing options...
radi8 Posted May 18, 2009 Share Posted May 18, 2009 Put the data from the query into an array with the last element being the sum, and then sort the array on the sum column. It is easy to display the array after that. Quote Link to comment https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/#findComment-836625 Share on other sites More sharing options...
premiso Posted May 18, 2009 Share Posted May 18, 2009 $sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists, SUM(goals + assists) as points FROM ww_players ORDER BY points DESC LIMIT 0,9"; If the order by does not work, try this: ORDER BY SUM(goals + assists) DESC If it requires a GROUP BY, try this for that: GROUP BY player_id, player_number, forename, surname, games_played Sorry my SQL is shabby. Moving the the MySQL forum. Quote Link to comment https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/#findComment-836628 Share on other sites More sharing options...
keeps21 Posted May 18, 2009 Author Share Posted May 18, 2009 $sql = "SELECT player_id, player_number, forename, surname, games_played, goals, assists, SUM(goals + assists) as points FROM ww_players GROUP BY player_id ORDER BY points DESC, goals DESC LIMIT 0,10"; Cheers for your help. Solved with the code above. Quote Link to comment https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/#findComment-836636 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 SELECT player_id, player_number, forename, surname, games_played, goals, assists, goals + assists AS sum_points FROM ww_players ORDER BY sum_points DESC LIMIT 0,9 ? Quote Link to comment https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/#findComment-836637 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.