将列添加到查询中

时间:2012-10-22 15:22:40

标签: mysql

我有这张桌子

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

3 个答案:

答案 0 :(得分: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 with Demo

答案 1 :(得分: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 Demo

根据您的演示,所有用户都在[{1}}

答案 2 :(得分: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