MySql查询当月的每一天

(自用笔记整理)

查询当月的每一天,由1号开始,到当月结束日期

SELECT
date_add(DATE_ADD(curdate(), INTERVAL - DAY(curdate()) + 2 DAY), INTERVAL (cast( help_topic_id AS signed INTEGER ) - 1 ) DAY ) DAY 
from mysql.help_topic
where help_topic_id  < day(last_day(curdate()))
order by help_topic_id

返回结果:

查询当天起未来一个月(30天)的每一天 

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

返回结果:

新添加另一种查询方法:

SELECT date FROM
(select
DATE_ADD(t1.dayed,INTERVAL t2.number DAY) date from
(select DATE_ADD(curdate(),interval -day(curdate())+1 day) dayed) t1,
(SELECT @xi:=@xi+1 as number from 
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc1, 
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8) xc2,
(SELECT @xi:=-1) xc0 ) t2 
where DATE_ADD(t1.dayed ,INTERVAL t2.number DAY))time WHERE SUBSTRING(date,1,7)=SUBSTRING(CURDATE(),1,7) 

结果: 

一切美好的遇见都是命中注定~! 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值