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 7828693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:26:45+00:00 2026-06-02T10:26:45+00:00

Background: I want to create a database that can run a tournament of 1

  • 0

Background:

I want to create a database that can run a tournament of 1 vs 1 matchups. It needs to keep track of who won and lost each matchup and any comments about that matchup as well as decide the next unique matchup randomly.

Rules:

There are x number of players. Each player will eventually play every other player once, in effect covering all possible unique combinations of players.

Database Tables (with Sample data):

DECLARE @Players TABLE (
    ID INT PRIMARY KEY IDENTITY,
    Name VARCHAR(50)
)

ID Name  
-- ----- 
1  Alex  
2  Bob   
3  Chris 
4  Dave 

DECLARE @Matches TABLE (
    ID INT PRIMARY KEY IDENTITY,
    WinnerId INT,
    LoserId INT
)

ID WinnerId LoserId 
-- -------- ------- 
1  1        2       
2  4        2       
3  3        1    

DECLARE @Comments TABLE (
    ID INT PRIMARY KEY IDENTITY,
    MatchId INT,
    Comment VARCHAR(MAX)
)

ID MatchId Comment                        
-- ------- ------------------------------ 
1  2       That was a close one.          
2  3       I did not expect that outcome. 

Problem:

  • How can I efficiently query to get a single random match up that has not yet occurred?

The major problem is that the number of player can and will grow over time. Right now in my example data I only have 4 players which leaves 6 possible matches.

Alex,Bob
Alex,Chris
Alex,Dave
Bob,Chris
Bob,Dave
Chris,Dave

That would be small enough to simply keep grabbing 2 random numbers that correspond to the Player’s id and then check the matchups table if that matchup has already occurred. If it has: get 2 more and repeat the process. If it hasn’t then use it as the next matchup. However if I have 10,000 players that would be 49995000 possible matchups and it would simply become too slow.

Can anyone point me in the right direction for a more efficient query? I am open to changes in the database design if that would help make things more efficient as well.

  • 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-06-02T10:26:50+00:00Added an answer on June 2, 2026 at 10:26 am

    If you make an outer join between every possible pairing and those that have been played, then filter out the ones that have been played, you’re left with pairings that have not yet been played. Selecting a random one is then a trivial case of ordering:

    SELECT p1.Name, p2.Name FROM
      Players p1
      JOIN Players p2 ON (
        p1.ID < p2.ID
      )
      LEFT JOIN Matches ON (
           (WinnerId = p1.ID AND LoserId = p2.ID)
        OR (WinnerId = p2.ID AND LoserId = p1.ID)
      )
    WHERE Matches.ID IS NULL
    ORDER BY RAND()
    LIMIT 1;
    

    EDIT

    As noted by ypercube below, the above LIMIT syntax is MySQL specific. You may need to use instead the appropriate syntax for your SQL implementation – let us know what it is and someone can advise, if required. I know that in Microsoft SQL Server one uses TOP and in Oracle ROWNUM, but otherwise your Googling is probably as good as mine. 🙂

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

Sidebar

Related Questions

I want to create a web page background image that fills the entire background,
I want to create a background thread that's owned by an object. When that
I want to create a process control application. Events update the database and that
Background I have a Access 2010 database and want to create a pop-up progress
Background : I want to create a custom VB compiler, extending the original compiler,
I have a big div with a big background-image. Now I want to create
Background We want to run an FTP server on a Red Hat Enterprise OS.
I have an AsyncTask and on the background I want to start Service that
I have a webpage where users can create a database record and select a
I'm trying to create a script that runs in the background automatically and cycles

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.