选择给定时间段内类型的第一个记录

时间:2012-09-03 11:18:57

标签: mysql select

我有一个存储用户评论的数据库表:

comments(id, user_id, created_at)

从该表中,我想获得过去7天内第一次评论的用户数。


这是我到目前为止所拥有的:

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

这会给出已评论的用户数量,但不会考虑这些评论是否首先针对其用户。

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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)))