Question

I am trying to sort two tables like this:

1 | Gang 1 | Total wealth: $100,000
2 | Gang 2 | Total wealth: $97,000

The gang structure ("gang"):

gangID, gangName

The player structure ("players")

ID, Username, Gang, Money, Bank

I want to do a query on if a player is in gang, it will count the players Money + Bank to the total wealth. However, I am unsure how to do this, and I'd really like your help.

http://sqlfiddle.com/#!2/8f239

Was it helpful?

Solution

This is what u need

select 
g.gangID,
g.gangName,
concat('$ ',`tot_wealth`) as `Total wealth`
from gang g
inner join 
(
    select Gang,FORMAT(sum(Cash+Bank),0) as `tot_wealth`
    from players 
    group by Gang
)p
on p.Gang = g.gangID
group by g.gangID

http://sqlfiddle.com/#!2/8f239/4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top