MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 9th, 2000, 02:16 PM
Annie Annie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 15 Annie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have a table with 2 columns: score and person. I am trying to calculate the person with the highest score. I tried doing:

SELECT max(score), person FROM my_table GROUP BY person;

but that results in many returns, not just one. I also tried:

SELECT person FROM my_table WHERE score=max(score);

but that resulted in an error.

Is it possible to find the maximum score and the person associated with that score in one step? Or should I find the score first and then use the WHERE statement in second select statement to get the person that earned it?

Thanks in advance for in help you might be able to offer.

Reply With Quote
  #2  
Old May 9th, 2000, 04:29 PM
gbubani gbubani is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 10 gbubani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Annie:
I have a table with 2 columns: score and person. I am trying to calculate the person with the highest score. I tried doing:

SELECT max(score), person FROM my_table GROUP BY person;

but that results in many returns, not just one. I also tried:

SELECT person FROM my_table WHERE score=max(score);

but that resulted in an error.

Is it possible to find the maximum score and the person associated with that score in one step? Or should I find the score first and then use the WHERE statement in second select statement to get the person that earned it?

Thanks in advance for in help you might be able to offer.
[/quote]

Hi,

I guess SELECT MAX() is for grouping only.
Try the following query :

SELECT person,score FROM my_table ORDER BY score LIMIT 1;

Bye,

GBubani

Reply With Quote
  #3  
Old May 10th, 2000, 04:01 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 9
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan
hi,

you should issue the following command:


select person,score from my_table order by score desc limit 1;

<<sort all the rows descending by score and only get the first row using the LIMIT clause>>.

------------------

SR -
shiju.dreamcenter.net

Web developer from GOD's own country!!!!!


[This message has been edited by Shiju Rajan (edited May 10, 2000).]

Reply With Quote
  #4  
Old May 10th, 2000, 07:07 AM
gbubani gbubani is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 10 gbubani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Annie:
I have a table with 2 columns: score and person. I am trying to calculate the person with the highest score. I tried doing:

SELECT max(score), person FROM my_table GROUP BY person;

but that results in many returns, not just one. I also tried:

SELECT person FROM my_table WHERE score=max(score);

but that resulted in an error.

Is it possible to find the maximum score and the person associated with that score in one step? Or should I find the score first and then use the WHERE statement in second select statement to get the person that earned it?

Thanks in advance for in help you might be able to offer.
[/quote]

Hi,

I'm sorry. In my previous e-mail I missed the DESC clause in the ORDER BY statement. The final command should be :

SELECT person,score FROM my_table ORDER BY score DESC LIMIT 1;


Bye,

GBubani

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Selecting other columns in conjunction with max(column)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway