I want to combine about 3 MySQL query to one , but some complex . (below is my test querys and table datas example)
Table1: accounts
id | account | email | refer
11 | aasdasd | 5@as.a | 0
12 | gasd | 4@ds.a | 11
13 | xcsxs | 3@ss.a | 11
14 | cbasd | 2@as.a | 11
15 | asdv | 1@gs.a | 11
16 | sdfgx | 6@hs.a | 8
…
Table2: characters
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
…
My Tested Querys:
SELECT id FROM accounts WHERE refer='11'
to identify the accounts was refer with value ’11’
and second query to show and sum totaltime of accounts characters where was have refer with value 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
Whats i want:
I want to combine the above two query two one and have a result like below :
rank column is highest rank of character from account and totaltime is sum of all characters time of that account
Account | Email | Rank | TotalTime
gasd |4@ds.a | 456 | 33
xcsxs |3@ss.a | 789 | 41
cbasd |2@as.a | 0 | 0
asdv |1@gs.a | 456 | 13
second query with SUM I think have problem , I add it now .
try this
edit :
LOOK DEMO SQLFIDDLE