我是靠谱客的博主 辛勤白昼,这篇文章主要介绍mysql-通过年份,或每月求和,现在分享给大家,希望可以做个参考。

通过年份求和

复制代码
1
2
3
4
5
6
7
select sum(money) from biz_expend where DATE_FORMAT(create_time,'%Y')="2022" and family_id="111" and status="0" select * from biz_experimental_base_info where DATE(expr) SELECT DATE_FORMAT(CURDATE(),'%Y-%m') biz_experimental_param_value

求每月金额

复制代码
1
2
3
4
5
6
7
select sum(case month(create_time) when '1' then money else 0 end) as Jan, sum(case month(create_time) when '2' then money else 0 end) as Feb, sum(case month(create_time) when '3' then money else 0 end) as Mar, from biz_expend where year(create_time)='2022'

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
如果查询不到数据这里值为null的话,可以通过ifnull将它设置为0 select ifnull(sum(case month(create_time) when #{month} then money else 0 end),0) as money from biz_expend where year(create_time)="2022" and status= "0" and family_id=#{id} IFNULL(a,b) 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。

在这里插入图片描述

最后

以上就是辛勤白昼最近收集整理的关于mysql-通过年份,或每月求和的全部内容,更多相关mysql-通过年份内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部