IT序号网

[MySQL]获取当月每一天

leader 2021年06月14日 数据库 310 0

常用使用场景: 统计某个月(某个时间区间)内每一天的数据量 


   
    
  1. select date_add( curdate(), interval( cast(help_topic_id as signed integer) - 1) day) day
  2. from mysql.help_topic
  3. where help_topic_id < day( last_day( curdate()))
  4. order by help_topic_id

延伸用法: 获取一段时间内的每分钟


   
    
  1. set @stime = str_to_date( '2018-11-07 08:00', '%Y-%m-%d %H:%i');
  2. set @etime = str_to_date( '2018-11-07 08:10', '%Y-%m-%d %H:%i');
  3. select date_add(@stime, interval ( cast(help_topic_id as signed integer) - 0) minute) minute
  4. from mysql.help_topic
  5. where help_topic_id <= timestampdiff( minute, @stime, @etime)
  6. order by help_topic_id

顺便贴一下oracle的写法


   
    
  1. select trunc( sysdate, 'MM') + rownum - 1 day
  2. from dual
  3. connect by rownum <= to_number(to_char( last_day( sysdate), 'dd'))

原文地址:https://blog.csdn.net/reeye_/article/details/83655477

评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!