[英]Sum values (same column)
I have this table 我有这张桌子
user_id time_completed
4 86.30887
5 57.81364
5 35.50281
5 10.00000
5 74.19355
5 31.91489
6 15.00000
6 20.50000
I need to sum all the time for each user, something like this: 我需要一直汇总每个用户的信息,如下所示:
user_id time_completed
4 86.30887
5 209.42489
6 35.50000
This is how I get the first table: 这是我获得第一张桌子的方式:
$result = mysql_query ("SELECT user_id,time_completed FROM `mytable` ORDER BY `mytable`.`user_id` ASC;" )
Any idea? 任何想法?
EDIT: What if I need to replace user_id for the name in the following table (db_users)? 编辑:如果我需要将user_id替换为下表中的名称(db_users),该怎么办?
id username
1 admin
2 peter
3 tom
4 user
5 joey
6 helen
EDIT2: I've modified this table (db_users) and I want country also appears in the query. EDIT2:我已经修改了该表(db_users),并且我希望国家也出现在查询中。
id username country
1 admin ES
2 peter IT
3 tom US
4 user GB
5 joey GE
6 helen FR
Like this: 像这样:
user_id time_completed country
4 86.30887 GB
5 209.42489 GE
6 35.50000 FR
Take a look here: http://sqlfiddle.com/#!2/24d1b/11 在这里看看: http : //sqlfiddle.com/#!2/24d1b/11
you need to use SUM()
which is an aggregate function and group them by their user_id
您需要使用SUM()
这是一个聚合函数,并按其user_id
对其进行分组
SELECT user_ID, SUM(time_COmpleted) totalTime FROM tableName GROUP BY user_ID
UPDATE 1 更新1
SELECT b.username, SUM(time_COmpleted) totalTime FROM tableName a INNER JOIN db_users b ON a.user_id = b.id GROUP BY b.username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.