简体   繁体   English

如何从一张桌子中选择不同的

[英]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. 我想为每个帖子做一个观看次数,但是,如果某个特定用户阅读了一篇特定的文章超过一次,它仍被视为1个观看次数。

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. 这次查询返回的总数view_count,但是它包含多个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) . 尝试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. 请注意,不确定是应该是member_id还是user_id ,因为您在问题中已经互换使用了它。

For example, see Using DISTINCT and COUNT together in a MySQL Query . 例如,请参阅在MySQL查询中一起使用DISTINCT和COUNT

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 编辑:根据OP反馈,从子查询中将MAX(view_date)删除了无意义的组

Thanks to gareththegeek, by modifying his answer a bit i can resolve this. 感谢gareththegeek,通过稍微修改一下他的答案,我可以解决这个问题。 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. 无论如何,到目前为止,这似乎仍然有效..再次对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. 下面的查询结果仅在给定间隔为0时才有效。用其他值(例如1(代表昨天))测试时,得出的结果不准确。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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