gpt4 book ai didi

php - mysql获取每个帖子最频繁值的结果

转载 作者:行者123 更新时间:2023-11-30 22:50:10 26 4
gpt4 key购买 nike

我发现了一些类似的问题,答案很好,但我不知道如何将其应用到我的具体案例中。我有一个网站,用户可以在其中对最喜欢的帖子进行 1 到 6 的评分。每个数字都是不同的类别。

现在我需要知道每个帖子的最频繁投票。所以我需要计算每个帖子 ID 以及每个帖子 ID 的最频繁值。

之后我想更新另一个表中的每个结果。 (我现在还不知道如何解决这个问题,我对 Mysql 还不是很好)。

在这两列中,我需要知道每个帖子在 post_id 中存在的频率以及每个帖子最频繁的投票数是多少。

只是我的表的一个例子(值=投票)

value | post_id               
---------------
3 | 12
1 | 6
4 | 13
2 | 5
6 | 12
5 | 6

我需要这样的输出才能知道哪个帖子主要针对哪个类别投票。

post | most voted in this category               
---------------
1 | 3
2 | 5
3 | 6
4 | 1
5 | 4
6 | 6

表格中的每个帖子都需要这个。而且我需要更新另一个表中的每个帖子。我想我必须循环执行此操作。但我已经卡在了第一部分。

我只有这个。对于第一部分。

    <?php global $wpdb;

$test = $wpdb->get_results('SELECT posts_id, value, COUNT(posts_id) AS ActionCount
FROM rating_item_entry_value
GROUP BY posts_id
ORDER BY ActionCount DESC');
echo '<pre>';
print_r($test);
echo '</pre>';

这是我得到的输出

    Array
(
[0] => stdClass Object
(
[posts_id] => 0
[value] => 5
[ActionCount] => 7
)

[1] => stdClass Object
(
[posts_id] => 221
[value] => 3
[ActionCount] => 3
)

[2] => stdClass Object
(
[posts_id] => 197
[value] => 5
[ActionCount] => 2
)

[3] => stdClass Object
(
[posts_id] => 164
[value] => 3
[ActionCount] => 1
)

)

举个例子。

我不知道如何做得更好,尝试了很多但无法弄清楚。有没有人有一个很好的解决方案如何为每个 id 获取最频繁的数字? (也许如何保护变量中的结果以更新循环内另一个表中的每个帖子?)非常感谢您的帮助。问候

最佳答案

most frequently 表示按频率计数聚合和分组。您可以将其映射到您的问题:

select
x.amount,
count(*) as times -- I forgot that row
from
X x
group by
x.amount
order by
count(*) DESC

//编辑给你的意思是

select
post_id,
value,
count(*)
from
your_table
group by
post_id,
value
order by
count(*) desc

关于php - mysql获取每个帖子最频繁值的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com