So here’s a little background on the system. User’s battle and the winner is the one who wins the most rounds. I need help with possibly joining three tables. I have table user one, which stores the user info. Table match which stores match information. Table rounds which stores the winner of every rounds per match, so if a match has 5 rounds, then table rounds will have five rows for that match and will record the winner of each round.
Here’s some sample data:
Table user:
(userid is the primary key)
userid username
-----------------
1 Kevin
2 Sam
3 Steve
4 Matt
Table match:
(id is the primary key. challenger and challenged are both foreign keys to user.userid)
id challenger challenged rounds
-----------------------------------
1 2 3 3
2 1 2 1
3 2 3 3
4 2 4 1
Table rounds:
(all fields are the primary key. id is foreign key to match.id and winner is foreign key to user.userid)
id round winner
------------------
1 1 2
1 2 2
1 3 3
2 1 1
3 1 2
3 2 3
3 3 2
4 1 4
I’m trying to build a query that will output the following results:
winner won loser won
------------------------
Sam 2 Steve 1
Kevin 1 Sam 0
Sam 2 Steve 1
Matt 1 Sam 0
The above results shows the winner and loser of each of match. The won field shows the number of rounds won for that match for the winner and loser respectively.
Does anyone know how I can build the above query?
It relies on (Problems with NULL Values):
and here it is an advantage.