简体   繁体   中英

How can I get max sum in table with mysql

I have an SQL database with a table like this:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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