简体   繁体   中英

How to select distinct from just one table

I have a table with the following structure

view_id | user_agent | view_date | post_id | user_id

I would like to do a view-count for each post, however if a particular user read a particular article more than once it still considered as 1 viewcount.

So far i have come up with this simple query

SELECT member_id, COUNT(member_id) AS view_count
FROM gw_library_viewcount
WHERE DATE(view_date) = DATE_SUB(DATE(NOW()), INTERVAL 0 DAY)
GROUP BY member_id;

However it returns the number of viewcount for each individual user for today. Then i edit it so it becomes

SELECT COUNT(member_id) AS view_count
FROM gw_library_viewcount
WHERE DATE(view_date) = DATE_SUB(DATE(NOW()), INTERVAL 0 DAY)

This time the query returns the total number of view_count, however it includes multiple viewcount. I only want it to returns the number of viewcount for each post with a condition that if there is a user who view the article more than once, it still considered as one viewcount.

How can i achieve this result?

Cheers

Try COUNT(DISTINCT member_id) .

That is,:

SELECT COUNT(DISTINCT user_id) FROM gw_library_viewcount WHERE ... GROUP BY post_id;

Note, not sure if it should be member_id or user_id , since you have used it interchangeably in your question.

For example, see Using DISTINCT and COUNT together in a MySQL Query .

You could use a subquery to select unique post views, something like this:

SELECT post_id, COUNT(member_id) AS view_count
FROM (
     SELECT DISTINCT member_id, post_id, MAX(view_date) AS view_date
     FROM gw_library_viewcount
     GROUP BY member_id, post_id
) t
WHERE DATE(view_date) = DATE_SUB(DATE(NOW()), INTERVAL 0 DAY)
GROUP BY post_id

Edit: Removed non-sensical group by MAX(view_date) from subquery based upon OP feedback

Thanks to gareththegeek, by modifying his answer a bit i can resolve this. This is the query i am looking for.

The subquery basically is to show what article each member has read for a particular day (multiple visit/read still counts as one)

So it returns result like this:

member id | post id | view_date
1 ---------> 1 ------> some_date 
1 ---------> 2 ------> some_date
3 ---------> 1 ------> some_date

and so on....

From here, the i simply count the number of users. Anyway this seems to be working so far.. again thx to gareththegeek.

SELECT COUNT(member_id) AS view_count
FROM (
    SELECT DISTINCT member_id, post_id, MAX(view_date) AS view_date
    FROM gw_library_viewcount
    WHERE DATE(view_date) = DATE_SUB(DATE(NOW()), INTERVAL 0 DAY)
    GROUP BY member_id, post_id
) AS subquery

* Previous query

The result of the query below turns out only valid if given interval is 0. When tested with other value such as 1 (representing Yesterday), it gives inaccurate result.

SELECT post_id, COUNT(member_id) AS view_count
FROM (
     SELECT DISTINCT member_id, post_id, MAX(view_date) AS view_date
     FROM gw_library_viewcount
     GROUP BY member_id, post_id
) t
WHERE DATE(view_date) = DATE_SUB(DATE(NOW()), INTERVAL 0 DAY)
GROUP BY post_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM