查询一个月的全部日期

1.解析:查询一个月的全部日期

SELECT 
	ADDDATE(monthAndDay.first, allMonth.d - 1) as day, -- 这是每个月的全部天数 经过每一次的查找进行加 allMonth.d 会进行+1
	allMonth.d,                                        -- 这是allMonth 中查出的所有天数 一共31天 月份的天数用 where中的条件去控制
	allMonth.d-1 as a ,																 -- 该月的天数一共多少天
	monthAndDay.first,																 -- 这是每个月的第一天 
	monthAndDay.last  																 -- 这是每个月的最后一天,又或是称为 每个月的天数
FROM
    (SELECT 1 AS d UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
    SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL
    SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL
    SELECT 29 UNION ALL SELECT 30 UNION ALL SELECT 31) allMonth, -- 这是为了取 1-31号的数值
    (SELECT CONCAT('2021-11','-01') as first, DAY(LAST_DAY(str_to_date('2021-11','%Y-%m'))) AS last) monthAndDay  
		-- first 是这个月的第一天,last 这是每个月的最后一天,又或是称为 每个月的天数
WHERE allMonth.d <= monthAndDay.last  -- 月份的天数用 where中的条件去控制

2.最后呈现的结果 就这样使用就好啦

SELECT 
	ADDDATE(monthAndDay.first, allMonth.d - 1) as day
FROM
    (SELECT 1 AS d UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
    SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL
    SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL
    SELECT 29 UNION ALL SELECT 30 UNION ALL SELECT 31) allMonth,
    (SELECT CONCAT('2021-11','-01') as first, DAY(LAST_DAY(str_to_date('2021-11','%Y-%m'))) AS last) monthAndDay  		
WHERE allMonth.d <= monthAndDay.last 

3.传日期的参数时,把上面的日期替换掉就好了,

'2021-11'---------例如这个

4.如果是查询连续的两个月的 就用 UNION 就好了

注意: 一般 我们在使用的时候都是用连续的日期和 单选择月份的(当时上传图片失败了,后期再补上。),用UNION ALL的话就会出现查找一个月份 会出现60几天 这是 UNION ALL和UNION 的区别,重复的会去掉(UNION )。

SELECT t1.* from (SELECT ADDDATE(y.first, x.d - 1) as day1
FROM
    (SELECT 1 AS d UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
    SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL
    SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL
    SELECT 29 UNION ALL SELECT 30 UNION ALL SELECT 31) x,
    (SELECT CONCAT('2021-11','-01') as FIRST, DAY(LAST_DAY(str_to_date('2021-11','%Y-%m'))) AS last) y
WHERE x.d <= y.last
UNION 
SELECT ADDDATE(y.first, x.d - 1) as day1
FROM
    (SELECT 1 AS d UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL
    SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
    SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL
    SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL
    SELECT 29 UNION ALL SELECT 30 UNION ALL SELECT 31) x,
    (SELECT CONCAT('2021-12','-01') as FIRST, DAY(LAST_DAY(str_to_date('2021-12','%Y-%m'))) AS last) y
WHERE x.d <= y.last
) t1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值