MySQL查询连续的年、月、日、季度

1、sql

-- 1、查询连续的天
SELECT
	date_format( date_sub( '2022-08-10', INTERVAL ( @i := @i - 1 ) DAY ), '%Y-%m-%d' ) AS times 
FROM
	mysql.help_topic
	JOIN ( SELECT @i := 1 ) c 
WHERE
	help_topic_id <= (
	TIMESTAMPDIFF( DAY, '2022-08-10', '2022-08-20' ));
	
-- 2、查询连续的月
		SELECT
	date_format( date_sub( '2022-08-10', INTERVAL ( @i := @i - 1 ) MONTH ), '%Y-%m' ) AS times 
FROM
	mysql.help_topic
	JOIN ( SELECT @i := 1 ) c 
WHERE
	help_topic_id <= (
	TIMESTAMPDIFF( MONTH, '2022-08-10', '2022-12-20' ));
	
-- 3、查询连续的季度
	SELECT
	CONCAT_WS("-",if(QUARTER( date_sub( '2022-08-10', INTERVAL (@i) QUARTER ))=4 && @i != 1,@j := @j + 1,@j),QUARTER( date_sub( '2022-08-10', INTERVAL ( @i := @i - 1 ) QUARTER ))) times
FROM
	mysql.help_topic
	JOIN ( SELECT @i := 1,@j := YEAR('2022-08-10')) c
WHERE
	help_topic_id <= (
	TIMESTAMPDIFF( QUARTER, '2022-08-10', '2023-12-20' ));
	
	-- 4、查询连续的年
		SELECT
	date_format( date_sub( '2022-08-10', INTERVAL ( @i := @i - 1 ) YEAR ), '%Y' ) AS times 
FROM
	mysql.help_topic
	JOIN ( SELECT @i := 1 ) c 
WHERE
	help_topic_id <= (
	TIMESTAMPDIFF( YEAR, '2022-08-10', '2023-12-20' ));

	
	-- 5、业务数据
	SELECT 
    CONCAT(YEAR(s.created_time),"-", QUARTER(s.created_time)) AS quarter 
FROM order_sell s  
GROUP BY CONCAT(YEAR(s.created_time),"-", QUARTER(s.created_time))

你可能感兴趣的:(MySQL,mysql,数据库)