0

以下 MySQL 查询按用户获取不同日期的所有周数据:

SELECT user_id,
       CONCAT(follow_up_date, ' - ', follow_up_date + INTERVAL 6 day) AS week,
       GROUP_CONCAT(follow_up_date)
FROM   feedback_2
WHERE  follow_up_date > ''
GROUP  BY WEEK(follow_up_date),
          user_id

输出:

---------------------------------------------------------------
user_id |         week           | group_concat(follow_up_date)|
---------------------------------------------------------------
4        2012-08-28 - 2012-09-03 | 2012-08-28,2012-08-28,2012-08-29
14       2012-08-28 - 2012-09-03 | 2012-08-28 

但我的要求是,如何为同一用户分隔不同的日期。

---------------------------------------------------------------
user_id |         week           | group_concat(follow_up_date)|
---------------------------------------------------------------
4        2012-08-28 - 2012-09-03 | 2012-08-28,2012-08-28
4        2012-08-28 - 2012-09-03 | 2012-08-29
14       2012-08-28 - 2012-09-03 | 2012-08-28 
4

2 回答 2

1
SELECT  `user_id`, 
        CONCAT(`follow_up_date`, ' - ', `follow_up_date` + INTERVAL 6 DAY) AS f_week, 
        GROUP_CONCAT(`follow_up_date`) AS c_date
FROM feedback_2 
WHERE `follow_up_date` > ''
GROUP BY f_week, `user_id`
ORDER BY  `user_id`, f_week
于 2012-08-30T06:37:32.723 回答
1

未测试,但您可以尝试:

SELECT user_id,
       _CONCAT(DATE_SUB(follow_up_date, INTERVAL _DAYOFWEEK(follow_up_date)-3
                                        day),
       ' - ', DATE_SUB(follow_up_date, INTERVAL _DAYOFWEEK(follow_up_date)-3 day
              )
       + INTERVAL 6 day) AS week,
       GROUP_CONCAT(follow_up_date)
FROM   feedback_2
WHERE  follow_up_date > ''
GROUP  BY WEEK(follow_up_date)

请删除_表达式中部分单词开头的字符,以便查询正确。

于 2012-08-30T06:16:04.557 回答