3

I have a database table that stores user comments:

comments(id, user_id, created_at)

From that table, I want to get the number of users that have commented for the first time in the past 7 days.


Here's what I have so far:

SELECT COUNT(DISTINCT `user_id`) 
FROM `comments` 
WHERE `created_at` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()

This would give the number of users that have commented, but it would not take into consideration whether these comments are first for their users.

4

2 回答 2

3
SELECT COUNT(DISTINCT `user_id`) 
FROM comments c1
WHERE created_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
AND NOT EXISTS 
   (SELECT NULL 
   FROM comments c2 
   where c1.user_id = c2.user_id
   AND c2.create_at < DATE_SUB(NOW(), INTERVAL 7 DAY));
于 2012-09-03T11:22:29.387 回答
3
SELECT COUNT(DISTINCT user_id)
FROM comments AS c1
WHERE c1.created_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
AND NOT EXISTS (SELECT 1 FROM comments AS c2 
                WHERE c2.user_id = c1.user_id AND c2.created_at < c1.created_at)

NOT EXISTS子句检查相同的 user_id 是否具有较早 created_at 时间的记录。如果是这样,这意味着这不是他们第一次发表评论,因此我们应该对这个记录打折扣。

我保留DISTINCT user_id了,因为有可能同时创建两条评论。您也可以尝试以下方法,它只会为每个用户获取第一条记录,因此您可以取消 DISTINCT,但我不知道哪个更优化:

SELECT COUNT(*)
FROM comments AS c1
WHERE c1.created_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
AND NOT EXISTS (SELECT 1 FROM comments AS c2 
                WHERE c2.user_id = c1.user_id 
                AND (c2.created_at < c1.created_at 
                    OR (c2.created_at = c1.created_at AND c2.id < c1.id)))
于 2012-09-03T11:23:16.210 回答