简体   繁体   English

选择列的总和并显示前3个最大总和与限制LIMIT

[英]Select sum of column and display top 3 MAX Sum with limit LIMIT

I have bellow snippet table 我有波纹管摘要表

表

Here i have to sum vote top 3 values. 在这里,我必须对投票的前3个值求和。

Suppose product_id 3030 have sum of vote column is 8.1 and the 3671 sum is 5.2 假设product_id 3030vote总和栏为8.13671 sum5.2

here i have to get top 3 product_id which have max sum like bellow example output. 在这里,我必须获得前3个product_id,它们具有以下示例示例输出所示的最大和。

here max sum product_id id 3030 have sum 8.1 and second is 3671 have sum 5.2 这里最大总和product_id id 3030sum 8.1 ,第二个是3671sum 5.2

I Have tried bellow query but it not showing top 3 product_id Max Top 3 of sum vote. 我尝试了波纹管查询,但未显示总和最高的前3个product_id

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

Try this: 尝试这个:

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

The above query will return the top 3 products having the greatest SUM(vote) values. 上面的查询将返回SUM(vote)值最大的前3产品。

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

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