Talking about the difference between where and having in Mysql

 

When the aggregation function is used in the SQL statement, and then the record set of the aggregation function needs to be filtered, the Having clause needs to be used at this time. The following article mainly introduces the difference between where and having in Mysql. Information, friends who need it can refer to the following

The OrderItems table represents order information, including fields: order number order_num and item_price product selling price, quantity product quantity.

order_num

item_price

quantity

a1

10

105

a2

1

1100

a3

1

200

a4

2

1121

a5

5

10

a6

1

19

a7

7

5

[Question] Write a SQL statement, aggregate according to the order number, and return all order numbers whose total order price is not less than 1000, and the final results are sorted in ascending order by order number.

Tip: total price = item_price multiplied by quantity

order_num

total_price

a1

1050

a2

1319

a4

2242

Let's take a look at the wrong way of writing:

1

2

3

4

5

select order_num, sum(item_price*quantity) total_price

from OrderItems

group by order_num

where total_price >= 1000

order by order_num

错误:SQL_ERROR_INFO: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where total_price >= 1000\norder by order_num' at line 4"

The above error is: illegal use of aggregate functions, aggregate functions cannot be used in the WHERE clause

Our total_price is equivalent to sum(item_price*quantity), and aggregate functions are used in the WHERE clause

correct mistakes

1

2

3

4

5

select order_num, sum(item_price*quantity) total_price

from OrderItems

group by order_num

having total_price >= 1000

order by order_num

That's right

Note when using having:

1. Rows are grouped.

2. Aggregate functions are used.

3. Groups that satisfy the conditions in the HAVING clause will be displayed.

4. HAVING cannot be used alone, it must be used together with GROUP BY.

Then there are the following differences with where:

1. WHERE can directly use the fields in the table as filter conditions, but cannot use the calculation functions in the group as the filter conditions; HAVING must be used in conjunction with GROUP BY, and the group calculation functions and group fields can be used as filter conditions.

2. If you need to obtain the required data from the associated table through connection, WHERE is to filter first and then connect, while HAVING is to connect first and then filter.

3. The second item leads to the high execution efficiency of WHERE, which cannot be filtered by the calculation function in the group, while HAVING can use the calculation function in the group, and the execution efficiency is low.

where, aggregate function, having execution sequence after from:

where > aggregate function > having

If you need to filter the results of the group by aggregation function, you can only use having. The Having statement is usually used in conjunction with the Group by statement to filter the result set returned by the Group by statement. The existence of the Having statement makes up for the deficiency that the Where keyword cannot be used in conjunction with aggregate functions.

For example: Query the student ID and average grade of students whose average grade is greater than 60 points

select s_id, AVG(s_score) s_avg from Score group by s_id Having AVG(s_score) > 60;

It can also be written as follows:

select s_id, AVG(s_score) s_avg from Score group by s_id Having s_avg > 60;

If you change Having to where, an error will be reported, because the execution order of where is greater than that of the aggregate function.

Reposted from: Micro reading   https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131865657