吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 865|回复: 1
收起左侧

[学习记录] MYSQL 获取本周、本月每天,本季度、近半年、本年、近一年的每个月份

  [复制链接]
fzwanglz 发表于 2023-3-14 21:03
1.日期范围获取每天(截止结束日期)

SELECT
    DATE_ADD( '2021-04-04', INTERVAL CAST( help_topic_id AS signed INTEGER ) DAY ) AS `date`
FROM
    mysql.help_topic
WHERE
    help_topic_id < DATEDIFF( '2021-07-04', '2021-04-04' ) + 1;

2. 日期范围获取每月(截止结束日期所在月)

SELECT
    DATE_FORMAT( DATE_ADD( '2021-04-04', INTERVAL CAST( help_topic_id AS signed INTEGER ) MONTH ), '%Y-%m' ) AS `month`
FROM
    mysql.help_topic
WHERE
    help_topic_id < TIMESTAMPDIFF( MONTH, '2021-04-04', '2021-07-04' ) + 1;

3.本周每天(截止今日)

SELECT
    DATE_FORMAT(DATE_ADD( DATE_SUB( NOW(), INTERVAL WEEKDAY( NOW()) + 0 DAY ), INTERVAL CAST( help_topic_id AS signed INTEGER ) DAY ), '%Y-%m-%d' ) AS `date`
FROM
    mysql.help_topic
WHERE
    help_topic_id <= WEEKDAY( NOW());

4.近7日每天(截止今日)

SELECT
    DATE_FORMAT( DATE_ADD( DATE_ADD( NOW(), INTERVAL - 6 DAY ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER ) DAY ), '%Y-%m-%d' ) AS `date`
FROM
    mysql.help_topic
WHERE
    help_topic_id < 7;

5.本月每天(截止今日)

SELECT
    DATE_ADD( DATE_ADD( NOW(), INTERVAL - DAY ( NOW())+ 1 DAY ), INTERVAL CAST( help_topic_id AS signed INTEGER ) DAY ) AS `date`
FROM
    mysql.help_topic
WHERE
    help_topic_id < DAY ( NOW());

6.近30日每天(截止今日)

SELECT
    DATE_FORMAT( DATE_ADD( DATE_ADD( NOW(), INTERVAL - 29 DAY ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER ) DAY ), '%Y-%m-%d' ) AS `date`
FROM
    mysql.help_topic
WHERE
    help_topic_id < 30;

7.本季度每月(截止所在月)

SELECT
    DATE_FORMAT( DATE_ADD( CONCAT( YEAR ( NOW()), '-0',(( QUARTER ( NOW()) - 1 )* 3 + 1 ), '-01' ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER )MONTH  ), '%Y-%m' ) as `month`
FROM
    mysql.help_topic
WHERE
    help_topic_id < MONTH ( NOW()) % 3;

8.近半年每月(截止所在月)

SELECT
    DATE_FORMAT( DATE_ADD( DATE_ADD( NOW(), INTERVAL - 5 MONTH ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER ) MONTH ), '%Y-%m' ) as `month`
FROM
    mysql.help_topic
WHERE
    help_topic_id < 6;

9.本年每月(截止所在月)

SELECT
    DATE_FORMAT( DATE_ADD( CONCAT( YEAR ( NOW()), '-01-01' ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER ) MONTH ), '%Y-%m' ) as `month`
FROM
    mysql.help_topic
WHERE
    help_topic_id < MONTH ( NOW());

10.近1年每月(截止所在月)

SELECT
    DATE_FORMAT( DATE_ADD( DATE_ADD( NOW(), INTERVAL - 11 MONTH ), INTERVAL CAST( help_topic_id AS SIGNED INTEGER ) MONTH ), '%Y-%m' ) as `month`
FROM
    mysql.help_topic
WHERE
    help_topic_id < 12;

免费评分

参与人数 3吾爱币 +4 热心值 +3 收起 理由
0106yingzi + 1 + 1 谢谢@Thanks!
ytw6176 + 2 + 1 谢谢@Thanks!
tyezhong + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

rainerosion 发表于 2023-3-15 00:38
挺实用的!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 00:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表