简体   繁体   中英

Get the sum of a column value for all rows with same value in different column

I need to know how to handle a pretty complex situation.

I have a system that allows users to vote up or down on comments that others make. I want to create a report of those with the most up votes based on all of their comments. The upvotes were not tracked in the users table, only in the comments table so it needs to go through the comments table and get the value in the vote column and output the sum of all of the vote column values for each userid. It then needs to order these and output the top 10.

Thanks in advance for help

If you post your users and comments table structure I could make a query. But it would be something like this:

SELECT SUM(votes) total, user_id FROM comments GROUP BY user_id ORDER BY total LIMIT 10

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