Total up MySQL Data?

流过昼夜 提交于 2019-12-13 06:40:40

问题


I'm making a profile page for a site that I'm making, and it there's a section to display 'Total Ratings', and the database structure I have is

username|song_id|plus|minus
---------------------------
example | 294398|001 | 000
egnum2! | 244202|000 | 001

So I need to count the amount of rows that contain a specified username and add them up, I can't find a MySQL command that I can understand too easily :S

Eg, I visit example's profile, and it counts all the number of times his name appears in the table example above, and give me a number, in the example above, example has 1 total rate by 'example' in the same way, i'll have to do another where I count all his plus rates and minus rates. (those are 1/0, I put in triple digits cause it wouldn't fit lol)


回答1:


Use a GROUP BY query and aggregate functions:

select username, count(*), sum(plus), sum(minus)
from your_table
where username = 'example'
group by username;


来源:https://stackoverflow.com/questions/6717266/total-up-mysql-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!