[Database] The usage difference between where and having in MySQL

The difference between the usage of where and having in MySQL

Having generally follows the group by and performs part of the record group selection to work.
where is to perform all the data to work.
Furthermore, having an aggregate function can be used, such as having sum(qty)>1000

1. The usage of having

The having clause allows us to filter all kinds of data after grouping. The where clause filters records before aggregation, which means that it acts before the group by and having clauses. The having clause filters the group records after aggregation. My understanding is that there is no such data in the real table, and these data survive through some functions.

// SQL实例
# 显示每个地区的总人口数和总面积
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. After dividing into groups, use aggregate functions to perform operations on different fields (one or more records) in each group.

// SQL实例
# 显示每个地区的总人口数和总面积.仅显示那些面积超过1000000的地区。

SELECT region, SUM(population), SUM(area)
FROM bbc
GROUP BY region
HAVING SUM(area)>1000000

Here, we cannot use where to filter regions exceeding 1,000,000, because such a record does not exist in the table.
On the contrary, the having clause allows us to filter the groups of data

// MySQL判断某个字段的长度:
select home_page from aaa where char_length(trim(home_page))<10 and char_length(trim(home_page))>1;

2. The difference between where and having clauses in MySQL

Both the where and having clauses in mysql can achieve the function of filtering records, but their usage still has some differences.Example
: Use the group by and having clauses to combine to find out the unique records
SQL is as follows:

select uid,email,count(*) as ct from `edm_user081217` GROUP BY email

Then look at this, it's easy to understand

select uid,email,count(*) as ct from `edm_user081217` GROUP BY email HAVING ct > 1

First use group by to group emails, and use having to filter those greater than 1, so that the 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 is different. The WHERE clause acts on tables and views, and the HAVING clause acts on groups.
WHERE selects input rows before grouping and aggregation calculations (thus, it controls which rows enter the aggregation calculation), while HAVING selects grouped rows after grouping and aggregation. Therefore, the WHERE clause cannot contain aggregate functions; it is meaningless to try to use aggregate functions to determine which rows are input to aggregate operations. In contrast, the HAVING clause always contains aggregate functions. (Strictly speaking, you can write a HAVING clause that does not use aggregation, but doing so is just a waste of effort. The same conditions can be used more effectively in the WHERE phase.)
In the previous example, we can apply city names in WHERE Restricted because it does not require aggregation. This is more efficient than increasing the limit in HAVING, because we avoid grouping and aggregation calculations for rows that fail the WHERE check.
In summary:
having generally follows group by and performs part of the record group selection to work.
where is to perform all the data to work.
Furthermore, having an aggregate function can be used, such as having sum(qty)>1000

Guess you like

Origin blog.csdn.net/m0_37882192/article/details/109827572