Jump to content

[SOLVED] Value of two field - PHP MySQL


keeps21

Recommended Posts

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?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/158634-solved-value-of-two-field-php-mysql/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.