Collections:
Using Multiple Columns in GROUP BY in MySQL
Can Multiple Columns Be Used in GROUP BY in MySQL?
✍: FYIcenter.com
If you want to break your output into smaller groups, if you specify multiple column names or expressions in the GROUP BY clause. Output in each group must satisfy a specific combination of the expressions listed in the GROUP BY clause. The more columns or expressions entered in the GROUP BY clause, the smaller the groups will be.
The tutorial exercise below shows you how to break data into groups per "tag" and per year when they were created. Then the group function COUNT(*) is applied on each group:
mysql> SELECT tag, YEAR(created), COUNT(*) FROM fyi_links GROUP BY tag, YEAR(created); +------+---------------+----------+ | tag | YEAR(created) | COUNT(*) | +------+---------------+----------+ | 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)
⇒ Using Group Functions in the ORDER BY Clause in MySQL
⇐ Counting Duplicated Values in a Column in MySQL
2017-10-16, 2953🔥, 0💬
Popular Posts:
How To Use "IF ... ELSE IF ..." Statement Structures in SQL Server Transact-SQL? "IF ... ELSE IF ......
What is SQL Server Transact-SQL (T-SQL)? SQL Server Transact-SQL, also called T-SQL, is an extension...
How Fixed Length Strings Are Truncated and Padded in SQL Server Transact-SQL? When the length of the...
How To End a Stored Procedure Properly in SQL Server Transact-SQL? Where the end of the "CREATE PROC...
How to change the data type of an existing column with "ALTER TABLE" statements in SQL Server? Somet...