Passed a month to obtain statistical information for that month

   Statistics need to punch a habit, is required to pass in a time format such as "2019-07", acquired the entire month of statistics, there is no complement 0

Directly below the sql:

 

SELECT
    xx_date,
    MAX( SUM ) AS SUM 
FROM
    (
    SELECT
        d.xx_date AS xx_date,
        d.sum AS SUM 
    FROM
        (
        SELECT
            @num := @num + 1 num,
            0 AS `sum`,
            DATE_ADD( DATE_FORMAT( '2019-07-01', '%Y-%m-%d' ), INTERVAL @num DAY ) AS xx_date 
        FROM
            shopping_hibitRecord a,
            ( SELECT @num :=- 1 ) num_t 
        WHERE
            @num < ( SELECT DAYOFMONTH( LAST_DAY( '2019-07-01' ) ) - 1 ) 
        ) d UNION ALL
    SELECT
        DATE_FORMAT( ADDTIME, '%Y-%m-%d' ) AS `xx_date`,
        COUNT( * ) AS 'sum' 
    FROM
        `shopping_hibitRecord` 
    WHERE
        DATE_FORMAT( shopping_hibitRecord.`ADDTIME`, '%Y-%m' ) = '2019-07' 
        AND deleteStatus = FALSE 
        AND TYPE = 0 
        AND sendUser_id = 35920 
        AND habit_id = 298 
    GROUP BY
        xx_date DESC 
    ) b 
GROUP BY
    `xx_date` DESC

  This code is divided into two steps: first, get all days of the month passed in time, and statistics set to 0

     as follows:

    

SELECT
        d.xx_date AS xx_date,
        d.sum AS SUM 
    FROM
        (
        SELECT
            @num := @num + 1 num,
            0 AS `sum`,
            DATE_ADD( DATE_FORMAT( '2019-07-01', '%Y-%m-%d' ), INTERVAL @num DAY ) AS xx_date 
        FROM
            shopping_hibitRecord a,
            ( SELECT @num :=- 1 ) num_t 
        WHERE
            @num < ( SELECT DAYOFMONTH( LAST_DAY( '2019-07-01' ) ) - 1 ) 
        ) d 

The second step, screening punch card records information from our records in the table:

     

SELECT
        DATE_FORMAT( ADDTIME, '%Y-%m-%d' ) AS `xx_date`,
        COUNT( * ) AS 'sum' 
    FROM
        `shopping_hibitRecord` 
    WHERE
        DATE_FORMAT( shopping_hibitRecord.`ADDTIME`, '%Y-%m' ) = '2019-07' 
        AND deleteStatus = FALSE 
        AND TYPE = 0 
        AND sendUser_id = 35920 
        AND habit_id = 298 
    GROUP BY
        xx_date DESC 

The result is:

The third step, after the full connection group by time

SELECT
    xx_date,
    MAX( SUM ) AS SUM 
FROM
    (
    SELECT
        d.xx_date AS xx_date,
        d.sum AS SUM 
    FROM
        (
        SELECT
            @num := @num + 1 num,
            0 AS `sum`,
            DATE_ADD( DATE_FORMAT( '2019-07-01', '%Y-%m-%d' ), INTERVAL @num DAY ) AS xx_date 
        FROM
            shopping_hibitRecord a,
            ( SELECT @num :=- 1 ) num_t 
        WHERE
            @num < ( SELECT DAYOFMONTH( LAST_DAY( '2019-07-01' ) ) - 1 ) 
        ) d UNION ALL
    SELECT
        DATE_FORMAT( ADDTIME, '%Y-%m-%d' ) AS `xx_date`,
        COUNT( * ) AS 'sum' 
    FROM
        `shopping_hibitRecord` 
    WHERE
        DATE_FORMAT( shopping_hibitRecord.`ADDTIME`, '%Y-%m' ) = '2019-07' 
        AND deleteStatus = FALSE 
        AND TYPE = 0 
        AND sendUser_id = 35920 
        AND habit_id = 298 
    GROUP BY
        xx_date DESC 
    ) b 
GROUP BY
    `xx_date` DESC

结果就是我要的数据:

  这仅仅是一种解决方法。

 

Guess you like

Origin www.cnblogs.com/wyf-love-dch/p/11199087.html