gpt4 book ai didi

php - 选择列的总和并显示 top 3 MAX Sum with limit LIMIT

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:46 26 4
gpt4 key购买 nike

我有下面的代码片段表

table

在这里我必须对投票前 3 个值求和。

假设 product_id 3030 vote 列的总和为 8.13671 总和5.2

在这里,我必须获得前 3 个 product_id,它们具有最大总和,如下面的示例输出。

这里最大总和 product_id id 3030sum 8.1 第二个是 3671sum 5.2

我试过下面的查询,但它没有显示前 3 product_id 最高总票数的前 3 名。

$query = mysql_query("SELECT sum(vote) AS 'meta_sum',product_id FROM rating GROUP BY product_id ASC LIMIT 3") OR DIE(mysql_error())

最佳答案

试试这个:

SELECT SUM(vote) AS 'meta_sum', product_id 
FROM rating
GROUP BY product_id
ORDER BY 'meta_sum' DESC LIMIT 3

以上查询将返回具有最大 SUM(vote) 值的前 3 产品。

关于php - 选择列的总和并显示 top 3 MAX Sum with limit LIMIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37610047/

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