Solved

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

Posted on 2008-10-24
10
500 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:ushastry
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 26

Expert Comment

by:ushastry
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:ushastry
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 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…

831 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