GROUP BY
(Aggregate) Functions
970
This function returns
0
if there were no matching rows.
•
COUNT(expr)
[970]
Returns a count of the number of non-
NULL
values of
expr
in the rows retrieved by a
SELECT
statement. The result is a
BIGINT
value.
COUNT()
[970]
returns
0
if there were no matching rows.
mysql>
SELECT student.student_name,COUNT(*)
->
FROM student,course
->
WHERE student.student_id=course.student_id
->
GROUP BY student_name;
COUNT(*)
[970]
is somewhat different in that it returns a count of the number of rows retrieved,
whether or not they contain
NULL
values.
COUNT(*)
[970]
is optimized to return very quickly if the
SELECT
retrieves from one table, no other
columns are retrieved, and there is no
WHERE
clause. For example:
mysql>
SELECT COUNT(*) FROM student;
This optimization applies only to
MyISAM
tables only, because an exact row count is stored for
this storage engine and can be accessed very quickly. For transactional storage engines such as
InnoDB
and
BDB
, storing an exact row count is more problematic because multiple transactions may
be occurring, each of which may affect the count.
•
COUNT(DISTINCT expr,[expr...])
[970]
Returns a count of the number of rows with different non-
NULL
expr
values.
COUNT(DISTINCT)
[970]
returns
0
if there were no matching rows.
mysql>
SELECT COUNT(DISTINCT results) FROM student;
In MySQL, you can obtain the number of distinct expression combinations that do not contain
NULL
by giving a list of expressions. In standard SQL, you would have to do a concatenation of all
expressions inside
COUNT(DISTINCT ...)
[970]
.
•
GROUP_CONCAT(expr)
[970]
This function returns a string result with the concatenated non-
NULL
values from a group. It returns
NULL
if there are no non-
NULL
values. The full syntax is as follows:
GROUP_CONCAT([DISTINCT]
expr
[,
expr
...]
[ORDER BY {
unsigned_integer
|
col_name
|
expr
}
[ASC | DESC] [,
col_name
...]]
[SEPARATOR
str_val
])
mysql>
SELECT student_name,
->
GROUP_CONCAT(test_score)
->
FROM student
->
GROUP BY student_name;
Or:
mysql>
SELECT student_name,
->
GROUP_CONCAT(DISTINCT test_score
->
ORDER BY test_score DESC SEPARATOR ' ')
->
FROM student
->
GROUP BY student_name;
In MySQL, you can get the concatenated values of expression combinations. To eliminate duplicate
values, use the
DISTINCT
clause. To sort values in the result, use the
ORDER BY
clause. To sort in
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...