-3

我有这张桌子

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

我总结时间并替换另一个表(db_users)中的用户:

id          username        country
1           admin           ES
2           peter           IT
3           tom             US
4           user            GB
5           joey            GE
6           helen           FR

但我也需要国家行也出现:

user_id     time_completed      country
joey           209.42489            GE
user           86.30887             GB
helen           35.50000            FR

看看这里:http ://sqlfiddle.com/#!2/24d1b/11

4

3 回答 3

3

您只需添加该字段:

SELECT b.username, SUM(time_COmpleted) totalTime, b.country
FROM tableName a 
      INNER JOIN  db_users b
        ON (a.user_id = b.id)
GROUP BY b.username, b.country

请参阅带有演示的 SQL Fiddle

于 2012-10-22T15:24:16.010 回答
1

在这里,只添加缺少的字段

SELECT b.username, SUM(time_COmpleted) totalTime, b.country
FROM tableName a 
      INNER JOIN  db_users b
        ON (a.user_id = b.id)
GROUP BY b.username, b.country

SQLFiddle 演示

根据您的演示,所有用户都生活在GB

于 2012-10-22T15:23:43.427 回答
0

好吧,只需将国家添加到SELECT

SELECT b.username, SUM(time_COmpleted) totalTime, b.country
FROM tableName a 
    INNER JOIN  db_users b
    ON (a.user_id = b.id)
GROUP BY b.username
于 2012-10-22T15:24:54.053 回答