Instructions for filtering group records with the having clause in MySql

The usage of
having The having clause allows us to filter various data after grouping, and the where clause filters the records before aggregation, that is to say, it acts before the group by and having clauses. The having clause filters group records after aggregation.

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, which is the literal meaning of GROUP BY meaning. After grouping, aggregate functions are then used to operate on the different fields (one or more records) in each group.

2. Display the total population and total area of ​​each region. Show only those regions with area more than 1000000
SELECT region, SUM(population), SUM(area)FROM bbcGROUP BY regionHAVING SUM(area)>1000000
Here, we can't use where to filter regions with more than 1000000, because there is no such thing in the table a record. On the contrary, the having clause allows us to filter the grouped data

mysql determines the length of a field
select home_page from aaa table where char_length(trim(home_page))1;

The difference between where and having clauses in
mysqlIn mysql Both the where and having clauses can achieve the function of filtering records, but there are some differences in their usage. Let's look at an example:
use group by and having clauses to find unique records, the sql is as follows:
select uid,email,count(*) as ct from `edm_user081217` GROUP BY email
and then look at this, it is easy to understand select uid,email,count(*) as
ct from `edm_user081217` GROUP BY email HAVING ct > 1
group by groups emails, and uses having to filter those greater than 1, so that duplicate records are found. The following is

the difference between having and where :
Select city FROM weather WHERE temp_lo = (SELECT max(temp_lo) FROM weather) ;
The object of action is different. The WHERE clause works on tables and views, and the HAVING clause works on groups. WHERE selects the input rows before the grouping and aggregation calculations (thus, it controls which rows go into the aggregation calculation), while HAVING selects the grouped rows after the grouping and aggregation. Therefore, the WHERE clause cannot contain aggregate functions; there is no point in trying to use aggregate functions to determine which rows are input to the aggregate operation. Conversely, the HAVING clause always contains aggregate functions. (Strictly speaking, you could write the HAVING clause without the aggregate, but doing so would be a waste of effort. The same condition could be used more effectively for the WHERE stage.) In the previous example, we could apply the city name in the WHERE limit because it does not require aggregation. This is more efficient than adding a limit to HAVING, because we avoid grouping and aggregation calculations for rows that fail the WHERE check. To sum up: having generally follows group by and performs part of the record group selection to work. where is to execute all data to work. Furthermore, having can use aggregate functions, such as having sum(qty)>1000

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480756&siteId=291194637