Jump to content

Recommended Posts

Hi,

 

Using mysql 5.0,

I have a simple table like this :

 

CREATE TABLE records (
  id int(11) NOT NULL auto_increment,
  member varchar(250) NOT NULL,
  option1 varchar(30) NOT NULL,
  option2 varchar(30) NOT NULL,
  option3 varchar(30) NOT NULL,
  points int(11) NOT NULL default '0',
  time int(11) NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

 

 

I'm trying to run this query on this :

 

select member,max(points),time from records where option1= 'competition1' and option2 = 1 and option3 = 'l'  group by time

so that it gives me the highest point in each "time".

 

 

The max points of the result is correct according to the time, the only mistake is the member field. it is not giving me the member which had the max points, it is just giving me another member who hasn't the max.

 

So that I'm having the true value of max in each "time" but with the wrong member.

 

Why?

Link to comment
https://forums.phpfreaks.com/topic/155821-solved-problem-with-max/
Share on other sites

The thing that you have to remember is that this query will get more than one row. Each time the member and time fields will be outputted and then you will have an extra value which will be the max number of points for the entire table - which will always be the same.

 

I would suggest something like putting the max(points) in the WHERE clause. So you tell it to get it WHERE points=max(points) or something similar. This way it will get the row(s) where the points value is the maximum.

You'll need to join back to the table once you're found the "max" record to get the remaining details.

 

By a second query?

 

I tired  :

select r1.member,max(r1.points),r1.time from records as r1 inner join records as r2 on max(r1.points) = r2.points where r1.option1= 'competition1' and r1.option2 = 1 and r1.option3 = 'l'  group by r1.time

 

gives me invalid use of group function.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.