欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL: Having

程序员文章站 2022-05-10 14:22:02
...

1. Eg.

  

# Fetch the difference between shop_price and market_price, and list the goods whose difference is bigger than 200

# The first part
select *, (market_price - shop_price) as difference from goods;

# The whole part
# The below approach will calculate twice, poor performance.
select *, (market_price - shop_price) as difference from goods where (market_price - shop_price) > 200;
# The below approach will calculate only once, better performance.
select *, (market_price - shop_price) as difference from goods having difference > 200;

# The key point to understand above is to understand the difference between real table and result set/virtual table/view.

# "where" is only applicable to real table column
# "having" is applicable to view/alias/result set

# It is hard to differentiate the difference of real table and result only judging from the structure.