How to use having in SQL?
"Having" is used to filter statistics after "group by". Generally, "having" will be used together with "group by". When using it, you must first group by "group by" and then proceed. "Having" statistical filtering, such as determining whether the value of an aggregate function is greater than a certain value.
SQL Example
1. Display the total population and total area of each region.
SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region
First divide the returned records into multiple groups by region. This is the literal meaning of GROUP BY. After grouping, use aggregate functions to operate on different fields (one or more records) in each group.
2. Display the total population and total area of each region. Only those areas with an area greater than 1,000,000 are shown.
SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region HAVING SUM(area)>1000000
Here, we cannot use where to filter regions with more than 1,000,000, because such a record does not exist in the table.
On the contrary, the having clause allows us to filter each group of data into groups
mysql determines the length of a certain field:
select home_page from aaa表 where char_length(trim(home_page))<10 and char_length(trim(home_page))>1;
Recommended tutorial: "
MySQL TutorialThe above is the detailed content of How to use having in SQL?. For more information, please follow other related articles on the PHP Chinese website!