从两个表中获取最高值和总和

时间:2012-11-21 18:08:46

标签: php mysql

我希望显示具有最高排名字符和时间总和的帐户表格帐户中的统计信息只有一个查询1

我有以下数据的两个表:

表:帐户:

Id| Username |Stats
1 | player1  |1
2 | goodman  |1
3 | goodbat  |1
4 | ashasdd  |0

表:字符:

Guid| Account | Name | Rank | Time |
213 |    1    | fres |  2   |  51  |
214 |    2    | sdg2 |  3   |  12  |
215 |    2    | fgax |  4   |  99  |
216 |    3    | zFvx |  8   |  23  |
217 |    3    | Sgzs |  2   |  13  |

输出/结果(显示具有最高等级字符及其时间总和的帐户字符)

Username : player1 | Name: fres(Rank:2) |Time : 51
Username : goodman | Name: fgax(Rank:4) |Time : 111
Username : goodbat | Name: zFvx(Rank:8) |Time : 36

什么是简单的MySQL查询?

我的错误查询: (不工作)

SELECT a.username, a.email, c.name, SUM(c.time), c.rank
FROM `auth`.`account` a, `characters`.`characters` c
WHERE a.id=c.account
ORDER BY c.rank ASC
LIMIT 20

2 个答案:

答案 0 :(得分:1)

应该足够......我想。

SELECT c.Account, a.Username, c.Name, MAX(c.Rank) as maxrank, SUM(c.TIME) as sumtime  FROM characters c LEFT JOIN Accounts a ON a.Id=c.Account GROUP By c.Account;

答案 1 :(得分:0)

这样的事情可能会起作用

SELECT username, name, d.rank, c.Time
FROM (

    SELECT a.Username, b.account, MAX( b.Rank ) AS rank, SUM( b.Time ) AS TIME
    FROM accounts a
    INNER JOIN characters b ON a.ID = b.Account
    WHERE a.stats =1
    GROUP BY a.Username
) AS c
JOIN characters d ON d.account = c.account
AND d.rank = c.rank
ORDER BY rank

不确定如何只在一个选择中执行此操作,从那时起您可能会得到错误的名称,即未注册最高等级。