[MySQL]求月初及求月底,使用 DAYOFWEEK (), DATE_ADD (),
LAST_DAY( ),CURDATE(),WEEKDAY(),DATE_SUB(),MONTH(),DAY()


SELECT

DATE_FORMAT(CURDATE() ,'%Y/%m/01') ,    #月初
LAST_DAY(CURDATE()) #月底

※※※※※※※※※※求年初第一個禮拜一※※※※※※※※※※※
SELECT
         IF(DAYOFWEEK(getDate.firstDate) =2,
                  getDate.firstDate,
                  DATE_ADD(getDate.firstDate, INTERVAL 7-WEEKDAY(getDate.firstDate) DAY)
         ) AS firstMonday
FROM
         (SELECT
                  DATE_SUB(
                           DATE_SUB(
                                    mydate.todate,
                                    INTERVAL MONTH(mydate.todate)-1 MONTH),
                           INTERVAL DAY(mydate.todate)-1 DAY
         ) AS firstDate
         FROM
                  (SELECT IFNULL(?, CURDATE()) AS todate) AS mydate
         )AS getDate

 





 















其它文章


arrow
arrow
    創作者介紹
    創作者 PG Levin Li 的頭像
    PG Levin Li

    程式開發學習之路

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()