1. # MySQL中统计函数的基础语句
sum,count,avg,max ,min
这里要注意 count(*) 和 count(1) ,在 myisam 中两者没有什么大的区别
count(column名) :计算 该column 没有null 的行数
count(*): 查询的是所有的行数的绝对值,即空值 null 也将计算在里面
tips: 在这里要把 sum、count、 avg、max、min后面当做是表达式,5 个统计的基础语句
2. # having 语句及 having 与 where 的区别
having 语句 是在where语句中执行的操作后,在查询的结果的基础上重新筛选出
having 与 where 的区别;
where 是查询满足式子的所有结果
having 是在where 筛选的结果上再进行查询;
3. # 查询出商品积压的货款及积压的货款大于20000(080-having筛选 39:00)
select cate_id, sum(goods_name * goods_num) as ZHK from goods where 1
group by cate_id having ZHK > 2000;
# 查询 出市场价格比本店价格高,且高于200以上的商品
select goods_id ,market_price,shop_price , market_price - shop_price as discount
from goods
where (market_price - shop_price > 200) # 这里 相当于查询了2次 资源浪费
select goods_id, market_price,shop_price,market_price - shop_price as discount
from goods
having discount >200;