简体   繁体   English

计算MySQL中的投票频率

[英]Count the frequency of votes in MySQL

I am making a website where users can vote on which category a page is. 我正在建立一个网站,用户可以在该网站上对页面的类别进行投票。 They can vote that the page is in category a, b, c, or d. 他们可以投票将该页面归类为a,b,c或d。

I need to find the most commonly occurring category in the MySQL row out of all the votes. 我需要从所有投票中找到MySQL行中最常见的类别。

Each time a user submits their vote, it submits the "category" that they voted for, and the "page_id". 每次用户提交投票时,都会提交他们投票的“类别”和“ page_id”。

I have this so far: 到目前为止,我有:

SELECT    page_id, category
FROM      categories
GROUP BY  page_id

I cannot use a COUNT(*) WHERE category = 'a' then repeat it for each category because there is many more categories in the actual project. 我不能使用COUNT(*) WHERE category = 'a'然后对每个类别重复此操作,因为实际项目中还有更多类别。

something like 就像是

SELECT category, page_id, count(vote_id)
FROM categories
WHERE category in ('a', 'b', 'c', 'd')
GROUP BY category, page_id
ORDER BY count(vote_id) DESC
LIMIT 1

should do the trick. 应该可以。 I assume here the votes are individually stored in a separate row per vote. 我在这里假设票数分别存储在每票单独的行中。

It only looks in the cqtegory you're interested in, sorts with the most votes first and only returns the first one. 它只会在您感兴趣的cqtegory中查找,以投票数最多的顺序排在第一位,仅返回第一位。

If your table looks something like this: 如果您的表格如下所示:

SELECT * from categories;
+---------+----------+
| page_id | category |
+---------+----------+
|       1 | a        |
|       1 | b        |
|       1 | a        |
|       1 | c        |
|       1 | a        |
|       1 | b        |
|       1 | a        |
|       2 | d        |
|       2 | d        |
|       2 | c        |
|       2 | d        |
|       3 | a        |
|       3 | b        |
|       3 | c        |
|       4 | c        |
|       4 | d        |
|       4 | c        |
+---------+----------+
17 rows in set (0.00 sec)

Then you may want to try this query: 然后,您可能想尝试以下查询:

SELECT   c1.page_id, MAX(freq.total),
         (
            SELECT   c2.category
            FROM     categories c2
            WHERE    c2.page_id = c1.page_id
            GROUP BY c2.category
            HAVING   COUNT(*) = MAX(freq.total)
            LIMIT    1
         ) AS category
FROM     categories c1 
JOIN     (
            SELECT   page_id, category, count(*) total 
            FROM     categories 
            GROUP BY page_id, category
         ) freq ON (freq.page_id = c1.page_id) 
GROUP BY c1.page_id;

Which returns this: 哪个返回:

+---------+-----------------+----------+
| page_id | MAX(freq.total) | category |
+---------+-----------------+----------+
|       1 |               4 | a        |
|       2 |               3 | d        |
|       3 |               1 | a        |
|       4 |               2 | c        |
+---------+-----------------+----------+
4 rows in set (0.00 sec)

Compare the results with the actual frequency distribution: 将结果与实际频率分布进行比较:

SELECT page_id, category, COUNT(*) FROM categories GROUP BY page_id, category;
+---------+----------+----------+
| page_id | category | COUNT(*) |
+---------+----------+----------+
|       1 | a        |        4 |
|       1 | b        |        2 |
|       1 | c        |        1 |
|       2 | c        |        1 |
|       2 | d        |        3 |
|       3 | a        |        1 |
|       3 | b        |        1 |
|       3 | c        |        1 |
|       4 | c        |        2 |
|       4 | d        |        1 |
+---------+----------+----------+
10 rows in set (0.00 sec)

Note that for page_id = 3 , there is no leading frequency, in which case this query makes no guarantee on which category will be chosen in such a case. 请注意,对于page_id = 3 ,没有前导频率,在这种情况下,此查询不能保证在这种情况下将选择哪个类别。

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

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