mysql 获取每个 post 的最频繁值的结果
mysql get result of most frequent value of every post
我发现了一些类似的问题,答案很好,但我不知道如何将其应用到我的具体案例中。我有一个网站,用户可以在其中对最喜欢的 post 从 1-6 进行评分。每个数字都是不同的类别。
现在我需要知道每个 post 的最频繁投票。所以我需要计算每个 post id 和每个 post id 的最频繁值。
之后我想在另一个 table 中更新每个结果。 (现在不知道如何解决这个问题,我对 Mysql 还不是很好)。
这是两列,我需要知道每个 post 在 post_id 中存在的频率以及每个 post.[=16 中最频繁的投票数是多少=]
只是我的一个例子table(价值=投票)
value | post_id
---------------
3 | 12
1 | 6
4 | 13
2 | 5
6 | 12
5 | 6
我需要这样的输出才能知道哪个 post 主要投票给哪个类别。
post | most voted in this category
---------------
1 | 3
2 | 5
3 | 6
4 | 1
5 | 4
6 | 6
table 中的每个 post 我都需要这个。而且我需要在另一个 table 中更新每个 post。我想我必须循环执行此操作。
但我已经卡在了第一部分。
我只有这个。对于第一部分。
<?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 获取最频繁的数字? (也许如何保护变量中的结果以更新循环中另一个 table 中的每个 post?)非常感谢您的帮助。问候
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
我发现了一些类似的问题,答案很好,但我不知道如何将其应用到我的具体案例中。我有一个网站,用户可以在其中对最喜欢的 post 从 1-6 进行评分。每个数字都是不同的类别。
现在我需要知道每个 post 的最频繁投票。所以我需要计算每个 post id 和每个 post id 的最频繁值。
之后我想在另一个 table 中更新每个结果。 (现在不知道如何解决这个问题,我对 Mysql 还不是很好)。
这是两列,我需要知道每个 post 在 post_id 中存在的频率以及每个 post.[=16 中最频繁的投票数是多少=]
只是我的一个例子table(价值=投票)
value | post_id
---------------
3 | 12
1 | 6
4 | 13
2 | 5
6 | 12
5 | 6
我需要这样的输出才能知道哪个 post 主要投票给哪个类别。
post | most voted in this category
---------------
1 | 3
2 | 5
3 | 6
4 | 1
5 | 4
6 | 6
table 中的每个 post 我都需要这个。而且我需要在另一个 table 中更新每个 post。我想我必须循环执行此操作。 但我已经卡在了第一部分。
我只有这个。对于第一部分。
<?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 获取最频繁的数字? (也许如何保护变量中的结果以更新循环中另一个 table 中的每个 post?)非常感谢您的帮助。问候
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