Collections:
Counting Groups Returned from GROUP BY in MySQL
How To Count Groups Returned with the GROUP BY Clause in MySQL?
✍: FYIcenter.com
If you use the COUNT(*) function on groups returned with the GROUP BY clause, it will count the number of rows within each group, not the number of groups. If you want to count the number of groups, you can put the GROUP BY query into a subquery and apply the COUNT(*) function on the main query as shown in the following tutorial exercise:
mysql> SELECT tag AS Category, YEAR(created) AS Year, COUNT(*) AS Counts FROM fyi_links GROUP BY tag, YEAR(created); +----------+------+--------+ | Category | Year | Counts | +----------+------+--------+ | DBA | 2005 | 1 | | DBA | 2006 | 2 | | DEV | 2004 | 1 | | DEV | 2006 | 1 | | SQA | 2003 | 1 | | SQA | 2006 | 1 | +----------+------+--------+ 6 rows in set (0.00 sec) mysql> SELECT COUNT(*) FROM ( SELECT tag AS Category, YEAR(created) AS Year, COUNT(*) AS Counts FROM fyi_links GROUP BY tag, YEAR(created) ) groups; +----------+ | COUNT(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec)
⇒ Returning Top 5 Rows in MySQL
⇐ Using Subqueries in the FROM Clause in MySQL
2017-12-21, 2022🔥, 0💬
Popular Posts:
How To Use SQL*Plus Built-in Timers in Oracle? If you don't have a stopwatch/timer and want to measu...
How To Download Oracle Database 10g XE in Oracle? If you want to download a copy of Oracle Database ...
Is PL/SQL Language Case Sensitive in Oracle? PL/SQL language is not case sensitive: Reserved words a...
How to set the current database in SQL Server? Once you are connected to the SQL Server, you should ...
Where to find answers to frequently asked questions on Transaction Management: Commit or Rollback in...