mysql某个月常用查询

2020-08-07  本文已影响0人  归来_仍是少年

1.查询本月第一天

select date_add(curdate(),interval-day(curdate())+1 day) as date;

2.查询本月最后一天

SELECT last_day(curdate()) as date;

3.查询当前日期

select curdate();

4.查询下个月的第一天

select date_add(curdate() - day(curdate()) +1,interval 1 month );

5.查询当前月已过了几天

select day(curdate());

6.获取当前月的天数(先加一个月,再减今天是第几天,得到当前月的最后一天,最后求最后一天是几号)

select day(date_add( date_add(curdate(),interval 1 month),interval -day(curdate()) day ));

7.查询上个月的第一天

select date_sub(date_sub(date_format(now(),‘%y-%m-%d’),interval extract(day from now())-1 day),interval 1 month);

8.查询上个月的最后一天

select date_sub(date_sub(date_format(now(),’%y-%m-%d’),interval extract(day from now()) day),interval 0 month) as date;

9.查询某个月的所有日期

        SELECT
                    days
                FROM
                    (
                        SELECT
                            DATE_FORMAT( DATE_SUB( LAST_DAY( CONCAT( #{request.months}, '-01' ) ), INTERVAL xc - 1 DAY ), '%Y-%m-%d' ) AS days
                        FROM
                            (
                                SELECT
                                    @xi := @xi + 1 AS xc
                                FROM
                                    ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) xc1,
                                    ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) xc2,
                                    ( SELECT @xi := 0 ) xc0
                            ) xcxc
                    ) x0
                WHERE
                        x0.days >= (
                        SELECT
                            DATE_ADD( CONCAT( #{request.months}, '-01' ), INTERVAL - DAY ( CONCAT( #{request.months}, '-01' ) )+ 1 DAY ))
上一篇下一篇

猜你喜欢

热点阅读