Solved

How can I select multiple MAX() values withphp/mysql?

Posted on 2008-10-24
10
517 Views
Last Modified: 2013-12-13
Here is my db structure (simplified, of course):


userid     games     wins   points
123          17             3          58
478           15           2           45
368           14           2           71
219           13           1           62

What I want to return is the MAX value for games, wins, and points along with the playerid who earned it.

Sample required output:

mostwins     mostwinsuserid     mostpoints     mostpointsuserid       mostgames     mostgamesuserid
3                         123                          71                    368                           17                      123

Any help is appreciated.

0
Comment
Question by:CountOrlok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 26

Expert Comment

by:Umesh
ID: 22798083

try this.......
table name: sports

SELECT * FROM(
            (SELECT MAX(wins) AS mostwins FROM sports)A,
            (SELECT userid AS mostwinsuserid FROM sports WHERE wins = ( SELECT MAX(wins) FROM sports))B,
            (SELECT MAX(points) AS mostpoints FROM sports)C,
            (SELECT userid AS mostpointsuserid FROM sports WHERE points = ( SELECT MAX(points) FROM sports))D,
            (SELECT MAX(games) AS mostgames FROM sports)E,
            (SELECT userid AS mostgamesuserid FROM sports WHERE games = ( SELECT MAX(games) FROM sports))F
)
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 22798127
Assuming the table name is gameslog, this should work.

SELECT t1.wins AS mostwins, t1.userid AS mostwinsid, t2.points AS mostpoints, t2.userid AS mostpointsid, t3.games AS mostgames, t3.userid AS mostgamesid FROM gameslog t1, gameslog t2, gameslog t3 ORDER BY t1.wins DESC, t2.points DESC, t3.games DESC LIMIT 1;
0
 

Author Comment

by:CountOrlok
ID: 22799086
MacAnthony -

I tried your suggestion first as it seemed cleaner, but I got this error: mysql #1030 - Got error 28 from storage engine.

ushastry - your solutionthrew up some invalid syntax errors, but I cannot find where the problem is.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 26

Expert Comment

by:Umesh
ID: 22799162
Seems to be disk space issue...  especially temporary dir... free some space on /tmp dir and try above
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 22799284
From some quick googling, it seems that an error 28 is a ran out of disk space issue. How many records are in this table?

To save space, each result could be returned from a subselect. Mine might be causing an issue since it's a union join, the result would be number of records cubed since there are 3 tables referenced. If there are 1000 records in there, the ending result set would be 1 billion.
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 22799349
Using subqueries:

SELECT
( SELECT userid FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostwinsid,
( SELECT wins FROM gameslog ORDER BY wins DESC   LIMIT 1) AS mostwins,
( SELECT userid FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostpointsid,
( SELECT points FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostpoints,
( SELECT userid FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostgamesid,
( SELECT games FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostgames
0
 
LVL 11

Accepted Solution

by:
MacAnthony earned 500 total points
ID: 22799385
Ooops. copy paste issue.

SELECT
( SELECT userid FROM gameslog ORDER BY wins DESC LIMIT 1 ) AS mostwinsid,
( SELECT wins FROM gameslog ORDER BY wins DESC   LIMIT 1) AS mostwins,
( SELECT userid FROM gameslog ORDER BY points DESC LIMIT 1 ) AS mostpointsid,
( SELECT points FROM gameslog ORDER BY points DESC LIMIT 1 ) AS mostpoints,
( SELECT userid FROM gameslog ORDER BY games DESC LIMIT 1 ) AS mostgamesid,
( SELECT games FROM gameslog ORDER BY games DESC LIMIT 1 ) AS mostgames
0
 

Author Comment

by:CountOrlok
ID: 22799511
There are 2,700 records in the table with about 10 being added each day.

MacAnthony, your latest solution seems to do the trick. Thanks!
0
 
LVL 26

Expert Comment

by:Umesh
ID: 22801090
Seems to be point passing.... disgusting
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 22801186
I appreciate the completely unnecessary and unprofessional comment.

If you have a concern, you can certainly bring it up to the moderators and keep the tactless comments out of the posts.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question