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

  • SEARCH
  • Home
  • 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 4558672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:40:53+00:00 2026-05-21T17:40:53+00:00

I am building an application where users can sign up and then challenge each

  • 0

I am building an application where users can sign up and then challenge each other (any other). When user 1 has challenged user 2, the score that exists between them is updated and stored (default score is 1 towards each other user). Mutually, user 2 can challenge user 1 and this updates another score.

The simplest way to represent this information is a n*n matrix (with a blank diagonal bcs you don’t have a “score” against yourself). My question is : how do you store it using MySQL ? I was thinking of a table with 3 columns : challenger, challenged, score, but that would result in a n² size and such an exponential factor seems inappropriate for such a simple request. Is there another, intended way to handle matrixes with MySQL ?

Thanks

  • 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-21T17:40:53+00:00Added an answer on May 21, 2026 at 5:40 pm

    Your initial idea seems right.
    BTW n² is quadratic, not exponential.

    MySQL should deal just fine with this.

    CREATE TABLE score (
      id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
      player1_id integer NOT NULL,
      player2_id integer NOT NULL,
      score_p1 integer NOT NULL,
      score_p2 integer NOT NULL,
      chal_date date KEY NOT NULL,
      /*UNIQUE KEY p1p2 (player1_id, player2_id),*/
      FOREIGN KEY p1 (player1_id) REFERENCES player.id 
        ON DELETE CASCADE ON UPDATE CASCADE,
      FOREIGN KEY p2 (player2_id) REFERENCES player.id 
        ON DELETE CASCADE ON UPDATE CASCADE)
     Engine=InnoDB;
    

    Note that you only create a row when an actual challenge has taken place and a score needs to be recorded. So you will have exactly one row per challenge. If the same players can challenge each other more then once you should remove the unique key p1p2.

    Usage examples

    Now if you want to know how many points a player has scored you do

    SELECT ifnull(sum(a.score_p1),0) + ifnull(sum(b.score_p2),0) AS total_score 
    FROM player
    INNER JOIN score a ON (a.player1_id = player.id) 
    INNER JOIN score b ON (b.player2_id = player.id)
    WHERE player.id = 587;
    

    If you want to know all the players a player has challenged you do

    SELECT score.player1 as opponent FROM score 
    WHERE score.player2 = 587
    UNION ALL
    SELECT score.player2 as opponent FROM score
    WHERE score.player1 = 587;
    

    If you do a small trick and add the following trigger, you can force that
    player1.id < player2.id

    DELIMITER $$
    
    CREATE TRIGGER bi_score_each BEFORE INSERT ON score FOR EACH ROW
    BEGIN
      DECLARE temp integer;
    
      /*Force player1.id to always be smaller than player2.id*/
      IF new.player1_id > new.player2_id THEN BEGIN
        SET temp = new.player1_id;
        SET new.player1_id = new.player2_id;
        SET new.player2_id = temp;
      END; END IF;
    END$$
    
    DELIMITER;
    

    Now you can simplify your query to see the cumulative score between two players.

    @p1:= 578;
    @p2:= 789;
    
    SELECT sum(score.score_p1) as score_p1
      ,sum(score.score_p2) as score_p2
    FROM score 
    WHERE score.player1_id = if(@p1<@p2,@p1,@p2) 
      AND score.player2.id = if(@p1>@p2,@p1,@p2);
    

    Use the following query to select all players a player has battled with the scores

      SELECT score.player2_id as opponent_id
        ,sum(score.score_p2) as opponent_score
        ,sum(score.score_p1) as player_score
        ,p2.name as opponent_name
        ,p1.name as player_name
      FROM score
      INNER JOIN player p2 ON (p2.id = score.player2_id) 
      INNER JOIN player p1 ON (p1.id = score.player1_id)
      WHERE player1_id = 587
      GROUP BY player2.id
    UNION ALL
      SELECT score.player1_id as opponent_id
        ,sum(score.score_p1) as opponent_score
        ,sum(score.score_p2) as player_score
        ,p1.name as opponent_name
        ,p2.name as player_name
      FROM score
      INNER JOIN player p2 ON (p2.id = score.player2_id) 
      INNER JOIN player p1 ON (p1.id = score.player1_id)
      WHERE player2_id = 587
      GROUP BY player1.id
    

    Hopes this gives you an idea of what is possible.

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

Sidebar

Related Questions

I am building an application that users can add, update data. Is there any
I am building an application that tracks karma that users give to each other.
I'm building a web-based productivity application that has to deal with modest user concurrency,
I am building an application that can have multiple users from multiple accounts. For
I am building an application where the users can download cooking recipes. Each recipe
I'm building a project. It's an application that users can add their extensions (DLL
So I am building an application that matches users. User models have 3 attributes
I'm building a web application that shows users interesting visualizations of their Gmail activity
I'm building a web application that guides my users through the configuration and installation
I am building a web application that uses the database for Users, Security/roles, and

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.