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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:42:36+00:00 2026-06-13T07:42:36+00:00

To realize a ranking system on my gameserver, I store some information about each

  • 0

To realize a ranking system on my gameserver, I store some information about each player in a MySQL database.
I’ve got the following database tables.

Table players with example data:

id | steamId | deaths
---+---------+-------
1  | asdja   | 2
2  | kfjsl   | 5

Table weapons with example data:

playerId | weaponId | kills
---------+----------+------
1        | 5        | 8
1        | 9        | 7
2        | 3        | 3
2        | 6        | 10
2        | 7        | 2

You see that I do not store the kills for each player in the players-table, because I could simply calculate it from the weapons-table.
I’m not very familiar with SQL queries, but I finished creating the following one to select a simple data set with following fields:

kills,
deaths,
kill-death-rate (kdrate),
count (maximum number of players),
rank (current ranking position, sorted by kills)

The query:

SELECT
    SUM(weapons.kills) AS `kills`,
    `deaths`,
    (SUM(kills) / IF(deaths, deaths, 1)) AS `kdrate`,
    (SELECT COUNT(*) FROM `players`) AS `count`,
    (
        SELECT
            COUNT(*)
        FROM
            (
                SELECT
                    p.id AS id2,
                    SUM(w.kills) AS kills2,
                    p.deaths AS deaths2,
                    p.steamId AS steamId2
                FROM
                    weapons AS w,
                    players AS p
                WHERE
                    p.id = w.playerId
                GROUP BY
                    p.id
            ) AS temp
        WHERE
            temp.kills2 >= (
                SELECT
                    SUM(weapons.kills) AS `kills`
                FROM
                    `players`,
                    `weapons`
                WHERE
                    players.id = weapons.playerId AND
                    `id` = 1
                GROUP BY
                  `id`
            )
        ORDER BY
            temp.kills2 DESC,
            temp.deaths2 ASC,
            temp.steamId2 ASC
    ) AS `rank`
FROM
    `players`
INNER JOIN
    `weapons`
ON
    players.id = weapons.playerId
WHERE
    `id` = 1
GROUP BY
    `id`

There are two problems:

1.) The query is horrible.

2.) Executing this query with the given example data results in “the same rank”.
I mean, both players get the same rank because I the amount of kills is the same on both players.
But instead, player 1 should be on rank 1 because he has less deaths than player 2.
I don’t know how I can realize this.

Thanks in advance!

EDIT:
@Manueru_mx gave me a basic idea of how to do it:
I’ve got the following code based on his answer:

SELECT
    id,
    kills,
    deaths,
    kdrate,
    COUNT(*) AS rank,
    (SELECT COUNT(*) FROM players) AS `count`
FROM
    (
        SELECT
            pys.id AS id,
            SUM(wps.kills) AS kills,
            pys.deaths AS deaths,
            (SUM(wps.kills) / pys.deaths) as kdrate
        FROM
            players pys
        INNER JOIN
            weapons wps
        ON
            pys.id = wps.playerid
        GROUP BY
            pys.id
        ORDER BY
            2 DESC,
            3,
            4 ASC
    ) AS tmp
WHERE
    id = 1

The only problem left is, that the rank is 1 in both cases.

  • 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-13T07:42:37+00:00Added an answer on June 13, 2026 at 7:42 am

    This is my “solution”. This query is more simple but get the results you are looking

    select 
    pys.steamID, SUM(wps.kills) as Kills,
    SUM(pys.deaths) as Deaths,
    (sum(wps.kills)/sum(pys.deaths)) as kdratio
    --,COUNT(pys.steamID) as PlayersC
    from players pys
    inner join  weapons wps on pys.id = wps.playerid
    group by pys.steamID
    order by 2 DESC, 3, 4 ASC
    

    Add the rank.

    SELECT
        id,
        kills,
        deaths,
        kdrate,
        ranking_usr as rank,
        (SELECT COUNT(*) FROM players) AS `count`
    FROM
        (
            SELECT
                @row := @row + 1 AS ranking_usr
                pys.id AS id,
                SUM(wps.kills) AS kills,
                pys.deaths AS deaths,
                (SUM(wps.kills) / pys.deaths) as kdrate
            FROM
                players pys
            INNER JOIN
                weapons wps
            ON
                pys.id = wps.playerid
            GROUP BY
                pys.id
            ORDER BY
                2 DESC,
                3,
                4 ASC
        ) AS tmp
    WHERE
        id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that each browser would implement it differently, but are there any references
I realize the following function calls are all same, but I do not understand
I realize there are several topics about this issue, but none provides a working
I realize that since UNIX sockets are platform-specific, there has to be some non-Java
I am trying to build a poor man's recommendation system for a online store.
I realize that we should use BigDecimal for all monetary values, but what about
I realize IDataReader is outdated and some view it as dirty code, but on
I realize that a billion people have asked about this error, but I've looked
I realize this question has been asked a dozen or more times and each
I realize that tinyint is a single byte integer (by the way, is it

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.