Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:33:00+00:00 2026-05-27T17:33:00+00:00

So here’s a little background on the system. User’s battle and the winner is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:33:01+00:00Added an answer on May 27, 2026 at 5:33 pm
    select 
        case when w1 > w2 then u1 else u2 end as winner,
        case when w1 > w2 then w1 else w2 end as won,
        case when w1 > w2 then u2 else u1 end as loser,
        case when w1 > w2 then w2 else w1 end as won
    from (
        select m.id, u1.username as u1, u2.username as u2, 
            count(r1.winner) as w1,
            count(r2.winner) as w2
        from match m
        join user u1 on m.challenger = u1.userid
        join user u2 on m.challenged = u2.userid
        join rounds r on r.id = m.id
        left join rounds r1 on r.id = r1.id and r.round = r1.round and r1.winner = u1.userid
        left join rounds r2 on r.id = r2.id and r.round = r2.round and r2.winner = u2.userid
        group by m.id, u1.username, u2.username
    ) t
    

    It relies on (Problems with NULL Values):

    Aggregate (summary) functions such as COUNT(), MIN(), and SUM() ignore NULL values.

    and here it is an advantage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is what is supposed to happen: The moment the user chooses an option
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is my error(if you need any more info just ask)- Error SQL query:
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is my work environment: Eclipse Juno as IDE with maven2 plugin on it
Here is what I am currently doing. PHP echo's out the recent post in
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.