gpt4 book ai didi

mysql - 找出 SQL 中的 AVG 列

转载 作者:行者123 更新时间:2023-11-29 04:50:30 25 4
gpt4 key购买 nike

我有这个 php/sql 查询:

$result = mysql_query("
SELECT r.item_id, AVG(rating) AS avgrating, count(rating) AS count, i.item, c.category
FROM ratings AS r
LEFT JOIN items AS i
ON r.item_id = i.items_id
INNER JOIN master_cat c
ON c.cat_id = i.cat_id
GROUP BY item_id
ORDER BY avgrating DESC
LIMIT 25;");

当我输出这个时,计数是正确的,它显示了某些项目收到了多少票。

我只是想添加一个 WHERE count >= 10 子句,但一切都中断了。显然,当有几千个项目时,有些会得到一票并拥有100%。但这不是一个好的指标。我想打印出至少有 10 票(或 count >= 10)的项目

最佳答案

你应该使用 having 而不是 where

SELECT 
r.item_id, AVG(rating) AS avgrating,
count(rating) AS count, i.item, c.category
FROM
ratings AS r
LEFT JOIN items AS i
ON r.item_id = i.items_id
INNER JOIN master_cat c
ON c.cat_id = i.cat_id
GROUP BY
item_id
HAVING
count >= 10
ORDER BY
avgrating DESC
LIMIT 25;

关于mysql - 找出 SQL 中的 AVG 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12251394/

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