★ wanayoo — archive 1999 http://www.devshed.com/Talk/Forums/Forum4/HTML/000447.htmlNouvelle recherche | Portail wanayoo
DevShed Menu
* DevShed Home
* Developer News
* DevShed Brain Dump




UBBFriend: Email This Page to Someone!
  DevShed Discussion Forums
  MySQL
  Selecting other columns in conjunction with max(column)

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   Selecting other columns in conjunction with max(column)
Annie
Junior Member
posted May 09, 2000 01:16 PM     Click Here to See the Profile for Annie     Edit/Delete Message Reply w/Quote
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.

gbubani
Member
posted May 09, 2000 03:29 PM     Click Here to See the Profile for gbubani   Click Here to Email gbubani     Edit/Delete Message Reply w/Quote
quote:
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.


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

Shiju Rajan
Member
posted May 10, 2000 03:01 AM     Click Here to See the Profile for Shiju Rajan   Click Here to Email Shiju Rajan     Edit/Delete Message Reply w/Quote
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).]

gbubani
Member
posted May 10, 2000 06:07 AM     Click Here to See the Profile for gbubani   Click Here to Email gbubani     Edit/Delete Message Reply w/Quote
quote:
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.


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

All times are MST (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | DevShed.com

Copyright © 1997-2000 ngenuity. All rights reserved.

Powered by: Ultimate Bulletin Board, Version 5.41a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.