GROUP_CONCAT()函数使用技巧

对日期分组之后使用GROUP_CONCAT()函数对该列字段可以一列查出所有数据。
SELECT
    GROUP_CONCAT(
        IFNULL(a.stat_date, '0000-00-00')
    ) AS stat_date,
    GROUP_CONCAT(IFNULL(a.comment_num, 0)) AS comment_num,
    GROUP_CONCAT(IFNULL(a.bad_comment_num, 0)) AS bad_comment_num,
    GROUP_CONCAT(IFNULL(a.comment_share_num, 0)) AS comment_share_num,
    GROUP_CONCAT(IFNULL(a.share_coupon_num, 0)) AS share_coupon_num,
    GROUP_CONCAT(IFNULL(a.record_intact_num, 0)) AS record_intact_num
FROM
    (
        SELECT
            stat_date,
            SUM(comment_num) AS comment_num,
            SUM(bad_comment_num) AS bad_comment_num,
            SUM(comment_share_num) AS comment_share_num,
            SUM(share_coupon_num) AS share_coupon_num,
            SUM(record_intact_num) AS record_intact_num
        FROM
            stat_platform_day
        WHERE
            1 = 1
        AND stat_date >= '2018-05-01'
        AND stat_date <= '2018-05-31'
        GROUP BY
            stat_date
    ) a;

浅析MySQL中concat以及group_concat的使用

你可能感兴趣的:(GROUP_CONCAT()函数使用技巧)