Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.14     Grouping rows together with GROUP BY

This is NOT the latest copy of this book; click here for the latest version.

Most types of data have repeating data that defines some element of it. For example, consider a table that stores the round score for members in a golf club - each time they complete a round (play all 18 holes) their score gets entered into the table, along with their member ID, date of play, etc. The scores are of course individual to themselves, but the member IDs will recur.

Now you can, as you know, limit output of those results by member by using the WHERE clause, but what if you want all the scores printed out, but with the member ID bringing them together?

Here's our table created in MySQL, outputted using "SELECT * FROM golfscores;":

+----+----------+-------+-------------+
| ID | MemberID | Score | DateEntered |
+----+----------+-------+-------------+
|  1 |        1 |    70 |  1089448558 |
|  2 |        1 |    74 |  1089448562 |
|  3 |       43 |    69 |  1089448569 |
|  4 |       13 |    77 |  1089448576 |
|  5 |        1 |    72 |  1089448584 |
|  6 |       13 |    80 |  1089448590 |
|  7 |        1 |    69 |  1089448599 |
+----+----------+-------+-------------+

Now, here's the same table again, this time extracted using the GROUP BY clause with "SELECT * FROM golfscores GROUP BY MemberID;":

+----+----------+-------+-------------+
| ID | MemberID | Score | DateEntered |
+----+----------+-------+-------------+
|  1 |        1 |    70 |  1089448558 |
|  4 |       13 |    77 |  1089448576 |
|  3 |       43 |    69 |  1089448569 |
+----+----------+-------+-------------+

As you can see, each MemberID gets printed out just once, and the Score and DateEntered fields that are printed are the values from the first row with each MemberID in. So, although MemberID 1 has scores of 74, 72, and 69, it's their first score of 70 that's printed - not very helpful!

However, in the creation of that query, MySQL does indeed read all the rows for each MemberID and could do something else with these values. By default, though, it just prints the first value - to do anything more we need to get into the realm of MySQL functions!





<< 9.3.13 Multiple WHERE conditions   9.3.15 MySQL functions >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow