五个统计函数(单独使用,意义不大,经常和分组group by组合使用)
max 最大 select max(shop_price) from goods;
min 最小 select min(shop_price) from goods;
sum 求和 select sum(shop_price) from goods;
avg 求平均值 select avg(shop_price) from goods;
count 所有值得行数有多少行
count(*)绝对行数null也计算在内
除此之外count(列名),计算这一列非null的行数
count使用
mysql> select * from test8;+------+------+| id | name |+------+------+| 1 | lisi | | 2 | NULL | +------+------+mysql> select count(*) from test8;+----------+| count(*) |+----------+| 2 | +----------+mysql> select count(name) from test8;+-------------+| count(name) |+-------------+| 1 | +-------------+
查询类型为4的库存
select sum(goods_number) from goods where cat_id=4;
group by
统计一下每个类型分组下的库存
mysql> select cat_id,sum(goods_number) from goods group by cat_id;+--------+-------------------+| cat_id | sum(goods_number) |+--------+-------------------+| 2 | 0 | | 3 | 203 | | 4 | 4 | | 5 | 8 | | 8 | 61 | | 11 | 23 | | 13 | 4 | | 14 | 9 | | 15 | 2 | +--------+-------------------+
不是标准的sql语句,逻辑上解释不通(每个类别cat_id里有很多goods_name)
不推荐 select goods_name ,sum(goods_number) from goods group by cat_id;
解释:在select a/b中必须在group by a/b/c语意上才没有问题
技巧:查询语句理解上从后面的条件过滤开始,先理解过滤条件,再看前面的执行
怪兽AI数字人
数字人短视频创作,数字人直播,实时驱动数字人
44 查看详情
以上就是mysql 统计函数和group by的内容,更多相关内容请关注PHP中文网(www.php.cn)!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/418377.html
微信扫一扫
支付宝扫一扫