where 表达式
表达式在哪一行成立,哪一行就被取出来
=,!=,<>,>,<,>=,<=,in,between and ,or ,not
group by
分组,一般和统计函数配合使用
max,min,avg,sum,count
查出价格最高的商品价格:select cat_id,max(shop_price) from ecs_goods;
查出价格最便宜的商品价格:select cat_id,min(shop_price) from ecs_goods;
查询该店一共有多少商品:select count(*) from ecs_goods;
查询该店库存总量:select sum(goods_number) from ecs_goods;
查出每个栏目价格最高的产品价格:select cat_id,max(shop_price) from ecs_goods group by cat_id;
查出每个栏目下积压的货款:
having表达式
数据在表中存储,表存在硬盘或内存中以文件形式存在
where就是针对表文件发挥作用,
where查询出来的结果可以看成是一张表,其文件一般临时存在缓冲区
having是针对查询的结果发挥的作用
作用:排序
可以针对字段,升序降序排列,
有可能一个字段查不出来结果,可以选择其他字段继续排序
查询栏目为3的卖价比市场价便宜200元的产品
> select goods_id,cat_id,goods_name,market_price - shop_price as nowPrice from ecs_goods where cat_id = 3 having nowPrice > 200 ;
order by
查询某个栏目下积压货款大于2w的栏目以及该栏目下积压的货款总和
> select cat_id,sum(shop_price*goods_number) as k from ecs_goods group by cat_id having k > 20000;
查询出每人挂科两门及两门以上和不及格的平均分
>select name,sum(chengji < 60 ) as gk,avg(chengji) as pj from fenshu group by name having gk>=2
limit 偏移量,取出的条目
可以把列名当做变量看待,可以使用数学公式进行计算
select cat_id,sum(shop_price*goods_number) from ecs_goods group by cat_id;
总结:
子查询
1、where型查询
内层的查询结果作为外层查询的比较条件
查询每个栏目价格最高的产品:
>select * from (select goods_id,goods_name,shop_price,cat_id from goods order by cat_id asc,shop_price desc) as tmp group by cat_id
2、from型子查询
把内层的查询结果供外层再次查询
注意,内层的查询结果看成临时表,加"as 临时表"
3、exists型查询
把外层的查询结果带入到内层,看内层是否成立
select * from category where exists(select * from goods where goods.cat_id = category.cate_id);
查询有商品的栏目:
>select cat_id,cat_name from ecs_category where exists (select * from ecs_goods where ecs_goods.cat_id = ecs_category.cat_id);