黑马程序员技术交流社区

标题: mysql查询 [打印本页]

作者: qaz1070608974    时间: 2016-4-9 13:10
标题: mysql查询
排序
1.查询所有的商品,按价格进行排序.(asc-升序,desc-降序)
    * select * from product order by price;
2.查询名称有新的商品的信息并且按价格降序排序.
    * select * from product where pname like '%新%' order by price desc;
聚合
常用的聚合函数: sum(),avg(),max(),min(),count();
1.获得所有商品的价格的总和:select sum(price) from product;
2.获得所有商品的平均价格:select avg(price) from product;
3.获得所有商品的个数:select count(*) from product;
分组
1.根据cno字段分组,分组后统计商品的个数.
    * select cid,count(*) from product group by cid;
2.根据cno分组,分组统计每组商品的平均价格,并且平均价格> 60;
    * select cid,avg(price) from product group by cid having avg(price)>60;




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2