mysql根据help_topic 获取指定日期

本文详细介绍了如何使用MySQL通过SQL查询来处理各种时间需求,包括近12个月、本年度、不同时间段、指定天数、月数和年数等,以及上月、本月、指定距离当前日期的时间范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql根据help_topic 获取指定日期

  • 可能会遇到统计上月、本年、上年、指定时间范围、24小时等要求,在数据库查询时通过sql进行时间切片,再通过连表的方式,将符合时间的数据与时间片对应
近12月(不包含本月)
		SELECT
            date_format(
	            date_sub( 
	            	curdate(), 
	            	INTERVAL (cast(help_topic_id AS signed) + 1) MONTH), 
            	'%Y-%m'
           	) MONTH
          FROM
             mysql.help_topic
          WHERE
              help_topic_id < 12
          ORDER BY
              help_topic_id
近12月(包含本月)
SELECT
	date_format(
		date_sub(
			curdate(),
			INTERVAL (cast(help_topic_id AS signed) - 0) MONTH),
		'%Y-%m'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>
	help_topic_id < 12
ORDER BY
	help_topic_id
本年:1月-12月
SELECT
	date_format(
		date_sub(
			ConCAT(YEAR(curdate()), '-12-31'),
			INTERVAL (
				cast(help_topic_id AS signed) - 0
			) MONTH
		),
		'%Y-%m'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>
	help_topic_id < 12
ORDER BY
	help_topic_id
根据时间范围查询指定的-天数
SELECT
   	date_format( date_sub( #{params.endDate}, INTERVAL (cast(help_topic_id AS signed) - 0) DAY), '%Y-%m-%d' ) MONTH
FROM
   	mysql.help_topic
 WHERE
   	help_topic_id  < timestampdiff(DAY, date_format(#{params.beginDate}, '%Y-%m-%d'), date_format( #{params.endDate}, '%Y-%m-%d')) + 1
 ORDER BY
   	help_topic_id desc
根据时间范围查询指定的-月数
SELECT
    date_format( date_sub( #{params.endDate}, INTERVAL (cast(help_topic_id AS signed) - 0) MONTH), '%Y-%m' ) MONTH
FROM
    mysql.help_topic
WHERE
    help_topic_id  < PERIOD_DIFF(date_format(#{params.endDate}, '%Y%m'), date_format( #{params.beginDate}, '%Y%m')) + 1
ORDER BY
    help_topic_id desc
根据时间范围查询指定的-年数
SELECT
     date_format( date_sub( #{params.endDate}, INTERVAL (cast(help_topic_id AS signed) - 0) YEAR), '%Y' ) MONTH
FROM
    mysql.help_topic
WHERE
    help_topic_id  < PERIOD_DIFF(date_format(#{params.endDate}, '%Y'), date_format(#{params.beginDate}, '%Y')) + 1
ORDER BY
    help_topic_id desc
上月:1号开始
SELECT
	date_sub(
		curdate(),
		INTERVAL (
			cast(help_topic_id AS signed) + date_format(curdate(),'%d')
		) DAY
	) month
FROM
	mysql.help_topic
WHERE
 # 符号转移需替换 <![CDATA[<]]>
 help_topic_id < day(LAST_DAY(date_sub(curdate(), INTERVAL(1) MONTH)))
ORDER BY
	help_topic_id
上月:当前日期 - 上月今日
SELECT
	date_format(
		date_sub(
			curdate(),
			INTERVAL (cast(help_topic_id AS signed) - 0) DAY),
		'%Y-%m-%d'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>
	help_topic_id < day(LAST_DAY(date_sub(curdate(), INTERVAL(1) MONTH))) + 1
ORDER BY
	help_topic_id
本月:1号 - 当前日期
SELECT
	date_format(
		date_sub(
			curdate(),
			INTERVAL (cast(help_topic_id AS signed) - 0) DAY),
		'%Y-%m-%d'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>
	help_topic_id < day(date_sub(curdate(), INTERVAL(1) MONTH))
ORDER BY
	help_topic_id
本月:1号 - 最后一天
SELECT
	date_format(
		date_sub(
			last_day(curdate()),
			INTERVAL (
				cast(help_topic_id AS signed) - 0
			) DAY
		),
		'%Y-%m-%d'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>
	help_topic_id < DAY (
		date_sub(
			last_day(curdate()),
			INTERVAL (0) MONTH
		)
	)
ORDER BY
	help_topic_id DESC
本月-当前日期 减 n天;n例如:近30天,近31天
SELECT
	date_format(
		date_sub(
			curdate(),
			INTERVAL (
				cast(help_topic_id AS signed) - 0
			) DAY
		),
		'%Y-%m-%d'
	) MONTH
FROM
	mysql.help_topic
WHERE
	# 符号转移需替换 <![CDATA[<]]>; 此处n数字替换成需要近n天距离当前的日期,如:30
	help_topic_id < n
ORDER BY
	help_topic_id DESC
输出0-23
SELECT
	help_topic_id 
FROM
	mysql.help_topic 
WHERE
	help_topic_id < 24
输出当前时间 24小时列表 yy-MM-dd hh
select 
	LEFT(
		DATE_ADD(CURDATE(),INTERVAL (CAST(help_topic_id as signed) - 0) hour),
		13
		) as MONTH
from
	mysql.help_topic
where
	help_topic_id < 24
ORDER BY help_topic_id
趋势分析使用模板
SELECT
    a.MONTH as time,
    ifnull(b.num, 0) as num
FROM
(
	# 这里使用 上面获取指定时间的sql
    SELECT
        date_format(date_sub(concat(YEAR(curdate()), '-12-31'),
    INTERVAL (cast(help_topic_id AS signed) - 0) MONTH),'%Y-%m') MONTH
    FROM
        mysql.help_topic
    WHERE
        help_topic_id <![CDATA[<]]> 12
    ORDER BY
        help_topic_id
) a
LEFT JOIN (
	# 这里是查询具体业务表 输出结果中一定要有日期和上面输出的日期(a.MONTH)匹配
    SELECT
    	c.number as num,
    	...
    	date_format(c.time, '%Y-%m') as time
    FROM
        tableC as c
    GROUP BY
    	# 格式化业务表 tableC 中的日期字段,保证和上面时间sql获取到的保持一致
        date_format(c.time, '%Y-%m')
) AS b
ON b.time = a.MONTH
ORDER BY a.MONTH
趋势分析自动适配模板
# 1.全局进行替换 #{beginTime}   #{endTime}
# 2.修改标注的地方
# 3.支持小时(%Y-%m-%d %H)、天数(%Y-%m-%d)、月数(%Y-%m)、年数(%Y)趋势的自适应适配
SELECT 
	a.MONTH as name,
	ifnull(b.num, 0) as value
FROM
	(

	SELECT
		case 
			when timestampdiff(MONTH, date_format(#{beginTime}, '%Y-%m-%d'),date_format(#{endTime}, '%Y-%m-%d')) = 0 then (
				case when timestampdiff(HOUR,date_format(#{beginTime}, '%Y-%m-%d %H:%i:%s '), date_format(#{endTime}, '%Y-%m-%d %H:%i:%s')) < 24 then (date_format(date_sub(#{endTime}, INTERVAL (cast(help_topic_id AS signed) - 0) HOUR),'%Y-%m-%d %H'))
				else date_format(date_sub(#{endTime}, INTERVAL (cast(help_topic_id AS signed) - 0) DAY), template.dateFormatTemplate) end 
			)
			when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then date_format(date_sub(#{endTime}, INTERVAL (cast(help_topic_id AS signed) - 0) MONTH), template.dateFormatTemplate)
			else date_format(date_sub(#{endTime}, INTERVAL (cast(help_topic_id AS signed) - 0) YEAR), template.dateFormatTemplate) end 
			as `MONTH`
	FROM
		mysql.help_topic topic LEFT JOIN (
		
			# 无问题
			SELECT
				case 
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) = 0 then '%Y%m%d'
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then '%Y%m'
					else '%Y' end as dateTemplate,
			  	case 
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) = 0 then 'DAY'
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then 'MONTH'
					else 'YEAR' end as dateModel,
				case 
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) = 0 then (
						case when timestampdiff(HOUR,date_format(#{beginTime}, '%Y-%m-%d %H:%i:%s'), date_format(#{endTime}, '%Y-%m-%d %H:%i:%s')) < 24 then '%Y-%m-%d %H' else '%Y-%m-%d' end
					)
					when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then '%Y-%m'
					else '%Y' end as dateFormatTemplate
			) template on 1=1
			
			
	WHERE
	# 无问题
		help_topic_id  < (select case 
			when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) = 0 then (
			case 
			when timestampdiff(HOUR,date_format(#{beginTime}, '%Y-%m-%d %H:%i:%s'), date_format(#{endTime}, '%Y-%m-%d %H:%i:%s')) < 24 then timestampdiff(HOUR, date_format(#{beginTime}, '%Y-%m-%d %H:%i:%s'), date_format(#{endTime}, '%Y-%m-%d %H:%i:%s')) + 1
			else timestampdiff(DAY, date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime},'%Y-%m-%d')) + 1 end )
			
			
			when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then timestampdiff(MONTH, date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) + 1
			else timestampdiff(YEAR, date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) + 1 end )
) a
LEFT JOIN (
		# 这里需要修改 业务表查询
    SELECT
    	count(c.id) as num,
    	date_format(c.evaluate_time, date.dateFormatTemplate) as time
    FROM
        tb_marketing_evaluate as c

		# 这里不能修改
		LEFT JOIN (
			SELECT case 
				when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) = 0 then 
				(case when timestampdiff(HOUR,date_format(#{beginTime}, '%Y-%m-%d %H:%i:%s'), date_format(#{endTime}, '%Y-%m-%d %H:%i:%s')) < 24 then '%Y-%m-%d %H' else '%Y-%m-%d' end)
				when timestampdiff(MONTH,date_format(#{beginTime}, '%Y-%m-%d'), date_format(#{endTime}, '%Y-%m-%d')) < 12 then '%Y-%m'
				else '%Y' end as dateFormatTemplate
		) date on 1 = 1
	
	# 这里需要修改 c.evaluate_time 改为对应的字段
	WHERE c.evaluate_time >= #{beginTime}
		and c.evaluate_time <= #{endTime}
    GROUP BY
        date_format(c.evaluate_time, date.dateFormatTemplate)
) AS b
ON b.time = a.MONTH
ORDER BY a.MONTH
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值