在mysql中显示周数

时间:2014-03-03 01:26:54

标签: mysql

我需要在mysql中显示月份的周数,目前我可以在执行此查询时获得一周的周数。我每周检索结果并需要显示周数(1月的第1周,1月的第2周,1月的第3周等)。任何人都可以帮我这么做吗?

SELECT  d.draftID_PK,
          d.clientID_FK,
          count(d.quoteNr) AS totalquote,
          DATE_FORMAT (d.draftDate,'%u  %M %Y') as draftDate,
          c.clientName
FROM draft d
INNER JOIN client c ON c.clientID_PK = d.clientId_FK
WHERE   d.draftDate<CURDATE() AND d.draftDate>'2013-01-05'
        AND c.clientName = '{$client_name}' 
GROUP BY DATE_FORMAT (d.draftDate,'%u')
ORDER BY d.draftDate DESC

1 个答案:

答案 0 :(得分:0)

我做了些什么,也许这可以帮助你,但周数因周开始而异,例如: 在巴西,本周从周日开始。所以,看看这是否可以解决您的问题。

SET @date:='2014-08-31';
SELECT 
    @first := date_add(
        date_add(
            LAST_DAY(@date),
            interval 1 DAY
        ),
        interval -1 MONTH
    ) AS first_day,
    @date,
    -- EXAMPLE 1
    WeekofYear(@date),
    WeekofYear(@first),
    WeekofYear(@date)-(WeekofYear(@first)-1) Num_Week,
    -- EXAPLE 2 - Works in Brazil perfectly
    Week(@date),
    Week(@first),
    Week(@date)-(Week(@first)-1) Num_Week_Brazil