总计MySQL数据? [英] Total up MySQL Data?

查看:175
本文介绍了总计MySQL数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个网站制作个人资料页面,它有一个部分显示总评级,我的数据库结构是

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

所以我需要计算包含指定用户名的行数,找不到我可以很容易理解的MySQL命令:S

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

例如,我访问示例的配置文件,它计数他的名字出现在上面的例子,并给我一个数字,在上面的例子中,例子有一个总速率的'example'以同样的方式,我将不得不另一个我计数所有他的加率和减速率。 (这些是1/0,我放在三位数,因为它不适合lol)

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)

推荐答案

使用 GROUP BY 查询和聚合函数

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

这篇关于总计MySQL数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆