/    Sign up×
Community /Pin to ProfileBookmark

Ordering a MySQL query.

I have the following php statment to query a DB:

[CODE]
$New = “SELECT *, Max(Hi_Score) FROM players LEFT JOIN playerstats ON players.id_Player = playerstats.id_Player WHERE players.Team1 = ‘$Team’ OR players.Team2 = ‘$Team’ OR players.Team3 = ‘$Team’ GROUP BY LName ORDER BY Hi_Score DESC”;
$result = mysql_query($New);

while($row = mysql_fetch_array($result)){
echo $row[‘LName’]. ” – “. $row[‘Max(Hi_Score)’];
echo “<br />”;
}

[/CODE]

I use the GROUP BY function to find the highest score for each player. I also want these players and their scores to be orders from the highest score to the lowest.

eg.
John = 14
Peter = 50
James = 25
Robert = 15

should look like:
Peter = 50
James = 25
Robert = 15
John = 14

The query does give me the high scores for each player but not in order.

How can I get the query to order it?

Please help me.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Mar 06.2007 — [code=php]SELECT *, Max(Hi_Score) as Hi_Score FROM players
LEFT JOIN playerstats ON players.id_Player = playerstats.id_Player
WHERE players.Team1 = '$Team' OR players.Team2 = '$Team' OR players.Team3 = '$Team'
GROUP BY LName
ORDER BY Hi_Score DESC[/code]
Copy linkTweet thisAlerts:
@gogelpotauthorMar 06.2007 — Thanks for the reply. If I use
[CODE]
SELECT *, Max(Hi_Score) as Hi_Score FROM players
LEFT JOIN playerstats ON players.id_Player = playerstats.id_Player
WHERE players.Team1 = '$Team' OR players.Team2 = '$Team' OR players.Team3 = '$Team'
GROUP BY LName
ORDER BY Hi_Score DESC
[/CODE]

I get the following error

Notice: Undefined index: Max(Hi_Score) in C:XitamiwebpagesQueryReport.php on line 25
[/QUOTE]


line 25 looks like this:
[CODE]
echo $row['LName']. " - ". $row['Max(Hi_Score)'];
[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Mar 06.2007 — The field name is defined as:[code=php]Max(Hi_Score) as Hi_Score[/code]Therefore, [code=php]echo $row['LName']. " - ". $row['Max(Hi_Score)'];[/code]should be:[code=php]echo $row['LName']. " - ". $row['Hi_Score'];[/code]
×

Success!

Help @gogelpot spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...