Collections:
Apply Filtering Criteria at Group Level in MySQL
How To Apply Filtering Criteria at Group Level in MySQL?
✍: FYIcenter.com
If you want to return only specific groups from the query, you can apply filtering criteria at the group level by using the HAVING clause inside the GROUP BY clause. Note group functions can also be used in HAVING conditions. The following tutorial exercise gives you some good HAVING example:
mysql> SELECT tag, COUNT(*), MIN(created), AVG(counts) FROM fyi_links GROUP BY tag HAVING AVG(counts) > 3.8; +------+----------+---------------------+-------------+ | tag | COUNT(*) | MIN(created) | AVG(counts) | +------+----------+---------------------+-------------+ | DEV | 2 | 2004-01-01 00:00:00 | 4.0000 | | SQA | 2 | 2003-01-01 00:00:00 | 7.0000 | +------+----------+---------------------+-------------+ 2 rows in set (0.00 sec) mysql> SELECT tag, COUNT(*), MIN(created), AVG(counts) FROM fyi_links GROUP BY tag HAVING COUNT(*) > 2; +------+----------+---------------------+-------------+ | tag | COUNT(*) | MIN(created) | AVG(counts) | +------+----------+---------------------+-------------+ | DBA | 3 | 2005-01-01 00:00:00 | 3.6667 | +------+----------+---------------------+-------------+ 1 row in set (0.00 sec)
⇒ Counting Duplicated Values in a Column in MySQL
⇐ Dividing Query Output into Groups in MySQL
2017-10-16, 1514🔥, 0💬
Popular Posts:
What is SQL Server Transact-SQL (T-SQL)? SQL Server Transact-SQL, also called T-SQL, is an extension...
How To Use "IF ... ELSE IF ..." Statement Structures in SQL Server Transact-SQL? "IF ... ELSE IF ......
What To Do If the StartDB.bat Failed to Start the XE Instance in Oracle? If StartDB.bat failed to st...
How To Calculate Age in Days, Hours and Minutes in SQL Server Transact-SQL? On many Web sites, news ...
What is SQL Server Transact-SQL (T-SQL)? SQL Server Transact-SQL, also called T-SQL, is an extension...