2013-01-15 31 views
0

我想结合约3 MySQL查询到一个,但一些复杂。 (下面是我的测试querys和表DATAS例子)结合两个查询,总和最大值列

表1:占

id | account | email | refer 
11 | aasdasd | [email protected] | 0 
12 | gasd | [email protected] | 11 
13 | xcsxs | [email protected] | 11 
14 | cbasd | [email protected] | 11 
15 | asdv | [email protected] | 11 
16 | sdfgx | [email protected] | 8 

...

表2:字符

guid |account| name | rank | time 
561  | 11 | asda | 945 | 12 
562  | 11 | asda | 746 | 19 
563  | 11 | asda | 452 | 1 
564  | 12 | asda | 123 | 15 
565  | 12 | asda | 456 | 18 
566  | 13 | asda | 123 | 6 
567  | 13 | asda | 789 | 18 
568  | 13 | asda | 123 | 17 
569  | 15 | asda | 456 | 13 
570  | 16 | asda | 123 | 15 
571  | 17 | asda | 456 | 16 

...
我测试过的查询小号

SELECT id FROM accounts WHERE refer='11' 

识别帐户是具有值“11” 和第二查询指示,总结TOTALTIME,其中被具有值11是指帐户字符:

SELECT a.account, a.email, SUM(c.time) , c.rank 
FROM accounts a, characters c 
WHERE a.id=c.account 
ORDER BY MAX(c.rank) DESC 

请告诉我我想要:
我想结合上面两个查询两个,并得到如下结果:
排名列是从帐户和总时间的字符的最高级别是所有总和该帐户的字符时间

Account | Email | Rank | TotalTime 
gasd |[email protected] | 456 | 33 
xcsxs |[email protected] | 789 | 41 
cbasd |[email protected] | 0 | 0 
asdv |[email protected] | 456 | 13 

第二个查询与SUM我认为有问题,我现在添加它。

回答

1

试试这个

SELECT a.account, a.email, c.rank,SUM(c.time) totaltime 
    FROM accounts a 
    INNER JOIN characters c 
    ON a.id = c.account 

    WHERE a.refer=11 
    ORDER BY MAX(c.rank) DESC 

编辑:

SELECT a.account, a.email, MAX(c.rank) rank,SUM(c.time) totaltime 
    FROM accounts a 
    left JOIN characters c 
    ON a.id = c.account 

    WHERE a.refer=11 
    group by a.account 
    ORDER BY MAX(c.rank) DESC 

LOOK DEMO SQLFIDDLE

+0

看看我的编辑答案 –

+0

组缺...感谢它的工作;)....'编辑: '快速回答记录:D谢谢;) – Root125