码迷,mamicode.com
首页 > 数据库 > 详细

mysql 根据起止年份 查询所选年份数据

时间:2019-01-12 14:16:50      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:ase   uil   日期   span   图片   col   pre   效果   gid   

1、时间字段是datetime类型

2、需要用到中间表,表结构和数据链接下载

https://pan.baidu.com/s/1sAQ78e3Ao-KwvYJlaipbKQ

3、前两个 adddate 月日需要在数据库写死 从每年的-01-01开始,-最后一个日期同理xxxx-12-31

4、sql

select
    bri.orgid,
    date_format(years.date, %Y) AS years,
    sum( case when bri.status = 1 then 1 else 0 end ) as num,
    sum( case when bri.status = 1 and bri.sex=1 then 1 else 0 end  ) as numman,
    sum( case when bri.status = 1 and bri.sex=2 then 1 else 0 end  ) as numgrid
    
from
    ( select adddate(2014-01-01, numlist.id) as date, 
        numlist.id 
    from ( select n100.i * 365 as id from num as n100 ) as numlist 
    where adddate(2014-01-01, numlist.id) <= 2019-12-31 ) years
    
left join building_resident_info bri on year (years.date) = year (bri.entrytime) and bri.status=1

and bri. status = 1
and bri.orgid like concat(37.02.82, %)
group by
    years.date

 

5、效果图

技术分享图片

 

  

 

如有疏漏,请指正!谢谢。

mysql 根据起止年份 查询所选年份数据

标签:ase   uil   日期   span   图片   col   pre   效果   gid   

原文地址:https://www.cnblogs.com/w-yu-chen/p/10259395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!