mysql 根据某一年 查询12个月的数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql 根据某一年 查询12个月的数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1990字,纯文字阅读大概需要3分钟。
内容图文
1、时间字段是datetime类型
2、需要用到中间表,表结构和数据链接下载
https://pan.baidu.com/s/1sAQ78e3Ao-KwvYJlaipbKQ
3、前两个 adddate 月日需要在数据库写死 从每年的-01-01开始,-最后一个日期同理xxxx-12-31
4、下面是sql
select concat( month(years.date),‘月‘) asyear, years.date, date_format(years.date, ‘%Y-%m‘) AS years, sum( casewhen bri.status =‘1‘then1else0end ) as num, -- 总数sum( casewhen bri.status =‘1‘and bri.sex=‘1‘then1else0end ) as numman, -- 男性数量sum( casewhen bri.status =‘1‘and bri.sex=‘2‘then1else0end ) as numgrid -- 女性数量from ( select adddate(‘2018-01-01‘, INTERVAL numlist.id month) as‘date‘, numlist.id from ( select n100.i *1as id from num as n100 ) as numlist where adddate(‘2018-01-01‘, INTERVAL numlist.id month) <=‘2018-12-31‘ ) years leftjoin building_resident_info bri on date_format(years.date, ‘%Y-%m‘) = date_format(bri.entrytime, ‘%Y-%m‘) and bri.status=‘1‘and bri. status =‘1‘and bri.orgid like concat(‘37.02.82‘, ‘%‘) groupby years.date
5、效果图如下
附带:
1、还有一种写法,是把12个月写死的
sql:
select case month(bri.entrytime) when‘1‘thensum(bri.status) else0endas 1月, casemonth(bri.entrytime) when‘2‘thensum(bri.status) else0endas 2月, casemonth(bri.entrytime) when‘3‘thensum(bri.status) else0endas 3月, casemonth(bri.entrytime) when‘4‘thensum(bri.status) else0endas 4月, casemonth(bri.entrytime) when‘5‘thensum(bri.status) else0endas 5月, casemonth(bri.entrytime) when‘6‘thensum(bri.status) else0endas 6月, casemonth(bri.entrytime) when‘7‘thensum(bri.status) else0endas 7月, casemonth(bri.entrytime) when‘8‘thensum(bri.status) else0endas 8月, casemonth(bri.entrytime) when‘9‘thensum(bri.status) else0endas 9月, casemonth(bri.entrytime) when‘10‘thensum(bri.status) else0endas 10月, casemonth(bri.entrytime) when‘11‘thensum(bri.status) else0endas 11月, casemonth(bri.entrytime) when‘12‘thensum(bri.status) else0endas 12月 from building_resident_info bri where date_format(bri.entrytime, ‘%Y‘) =‘2018‘and bri.orgid like concat(‘37.02.82.01‘, ‘%‘) and bri.status=‘1‘
效果:
如有疏漏,请指正!谢谢。
原文:https://www.cnblogs.com/w-yu-chen/p/10259374.html
内容总结
以上是互联网集市为您收集整理的mysql 根据某一年 查询12个月的数据全部内容,希望文章能够帮你解决mysql 根据某一年 查询12个月的数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。