Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-24
10
Medium Priority
?
528 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

885 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