MySQL列出当前月的每一天
因为工作的原因,要用MySQL列出当前月份每一天的日期,自己查了下网上资料都是列出最近一个月的日期的解决方案,自己根据查到的的方案,修改成了下面两个方案,在此记录下:
方案一:
SELECT
date_add(DATE_ADD(curdate(), INTERVAL - DAY(curdate()) + 2 DAY),
INTERVAL (cast( help_topic_id AS signed INTEGER ) - 1 ) DAY
) DAY
FROM
mysql.help_topic
WHERE
help_topic_id < DAY ( last_day( curdate( ) ) )
ORDER BY
help_topic_id
方案二:
SELECT
date_add(
CONCAT(YEAR(Date(curdate())),'-0',MONTH(Date(curdate())),'-','01'),
INTERVAL ( cast( help_topic_id AS signed INTEGER ) ) DAY
) DAY
FROM
mysql.help_topic
WHERE
help_topic_id < DAY ( last_day( curdate( ) ) )
ORDER BY
help_topic_id
根据方案做的当月考勤统计,代码如下:
SELECT
a.day,
aa.adt_statu
FROM
(
SELECT
date_add(
CONCAT( YEAR ( Date( curdate( ) ) ), '-0', MONTH ( Date( curdate( ) ) ), '-', '01' ),
INTERVAL ( cast( help_topic_id AS signed INTEGER ) ) DAY
) day
FROM
mysql.help_topic
WHERE
help_topic_id < DAY ( last_day( curdate( ) ) )
ORDER BY
help_topic_id
) a
LEFT JOIN atte_attendance aa ON DATE_FORMAT(aa.adt_in_time ,'%Y-%m-%d') = DATE_FORMAT(a.day ,'%Y-%m-%d')
ORDER BY a.day;
个人建议使用方案二,因为方案一自己只在7月做过测试,其他月份没有过测试,而且方案二确定性更强,不像方案一需要一些推算。由于自己手头缺少MySQL的函数手册,就不再进一步测试了。等到有时间再仔细看看