select 5种子句:
where 条件查询
group by 分组
having 筛选
order by 排序
limit 限制结果条数
---------------------
group by
group by 用于统计,
一般和max min sum avg count配合使用
max求最大
min求最小
sum求总和
avg求平均
count求总行数
注意:sum是求总和,而count是求总行数。
1.max
取出本店商品中的shop_price最大值:
seclect max(shop_price) from goods;
取出每个栏目中的shop_price最大值:
seclect cat_id,max(shop_price) from goods group by cat_id;
2.min
取出每个栏目中的shop_price最小值:
seclect cat_id,min(shop_price) from goods group by cat_id;
3.sum
取出每个栏目中的shop_price总和:
seclect cat_id,sum(shop_price) from goods group by cat_id;
4.avg
取出每个栏目中的shop_price平均值:
seclect cat_id,avg(shop_price) from goods group by cat_id;
5.count
取出每个栏目中的商品种类:
seclect cat_id,count(*) from goods group by cat_id;
---------------------
注意:要把列名当成变量名来看
本店每个商品价格比市场价格低多少钱:
seclect goods_name,market_price - shop_price from goods;
查询每个栏目下面积压的货款:
select cat_id,sum(shop_price * goods_number) from goods group by cat_id;
查询每个栏目下面积压的货款,变量也可以用别名,as ,如(sum(shop_price * goods_number) as huokuan):
select cat_id,sum(shop_price * goods_number) as huokuan from goods group by cat_id;
原文地址:http://1154179272.blog.51cto.com/10217799/1653234