Mysql 查询最近一年的数量sql

按月查询分月数量 , 某个月份没有的显示为0

SELECT
@s := @s + 1 AS `index`,
DATE_FORMAT( DATE_SUB( CURDATE(), INTERVAL @s MONTH ), '%Y-%m' ) AS `mon`
FROM
mysql.help_topic,
( SELECT @s := - 1 ) temp
WHERE
@s < 11
ORDER BY
mon DESC

 

查询最近七天的

SELECT
@s := @s + 1 AS `index`,
DATE_FORMAT( DATE_SUB( CURDATE(), INTERVAL @s DAY ), '%Y-%m-%d' ) AS `day`
FROM
mysql.help_topic,
( SELECT @s := - 1 ) temp
WHERE
@s < 6
ORDER BY
`day` DESC

 

 

posted @ 2021-12-20 11:33  Mr_xYu  阅读(658)  评论(0编辑  收藏  举报