简体   繁体   English

我如何在表中获取最大和

[英]How can I get max sum in table with mysql

I have an SQL database with a table like this: 我有一个带有这样的表的SQL数据库:

ObjID Rating
123   1
333   0
123   0
123   1
567   1

An Ojbect ID can show up in the table multiple times with different ratings, I need a way to select a "distinct" ObjID check all of its ratings and compare that to all other "distinct" ObjIDs and compare those ratings. 一个Ojbect ID可以以不同的评级多次出现在表中,我需要一种方法来选择“不同的” ObjID,检查其所有评级并将其与所有其他“不同的” ObjID进行比较,然后比较这些评级。 In the end I should have the 1st - 3rd ObjIDs with the over all highest ratings, in this example 123, 567 and 333 would be printed out. 最后,我应该获得具有最高总评分的第1-3 ObjID,在此示例中,将打印123、567和333。

So the rating of each object is the total sum of all ratings for that object in the table? 那么,每个对象的评级是表中该对象的所有评级的总和?

SELECT ObjID, SUM(Rating) AS TotalRating
FROM ObjectTable
GROUP BY ObjID
ORDER BY SUM(Rating) DESC
LIMIT 3

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

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