Selecting first records of a type in a given period

Posted by Emanuil Rusev on Stack Overflow See other posts from Stack Overflow or by Emanuil Rusev
Published on 2012-09-03T11:18:56Z Indexed on 2012/09/03 15:38 UTC
Read the original article Hit count: 198

Filed under:
|

I have a database table that stores user comments:

comments(id, user_id, created_at)

I want to get from it the number of users that have commented for the first time in the past week.


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 these users.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about select