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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:01:35+00:00 2026-06-13T19:01:35+00:00

I have two MySQL tables which look like these Teams id name abbr_name badge

  • 0

I have two MySQL tables which look like these

Teams

id  name    abbr_name   badge   slogan  captain
1   TEAM1   TM1     NULL    NULL    NULL
2   TEAM2   TM2     NULL    NULL    NULL
3   TAEM3   TM3     NULL    NULL    NULL
4   TEAM4   TM4     NULL    NULL    NULL
5   TEAM5   TM5     NULL    NULL    NULL
....

Match Entries

id  season  match_day   host    guest   time    host_score  guest_score  forfait mvp
1   1   1   3   4   NULL    10  2   NULL    43
2   1   1   5   6   NULL    2   2   NULL    16
3   1   1   9   10  NULL    6   3   NULL    49
4   1   1   2   1   NULL    5   5   NULL    29
5   1   1   7   8   NULL    4   2   NULL    52
6   1   2   1   4   NULL    7   3   NULL    75
....

What i would like to do is to make a query which can give me all the rows with values

Team_Id, Team_name, Team_N_Match_Played, Team_N_Match_Won, Team_N_Match_Lost

Is that possible without manipulating all the data with PHP and using MySQL queries directly instead?
I have no idea how I can fo that in MySQL.

  • 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-13T19:01:36+00:00Added an answer on June 13, 2026 at 7:01 pm

    MatchEntries = dc_match
    Teams = dc_teams

    SELECT
        team_id,
        name,
        SUM(P) AS played,
        SUM(W) AS won,
        SUM(D) AS draw,
        SUM(L) AS lost,
        SUM(PTS) AS points
    FROM(
        SELECT
            host team_id,
            CASE
                WHEN host_score IS NULL THEN 0
                ELSE 1
            END P,
            IF (host_score > guest_score,1,0) W,
            IF (host_score = guest_score,1,0) D,
            IF (host_score < guest_score,1,0) L,
            CASE 
                WHEN host_score > guest_score THEN 3
                WHEN host_score = guest_score THEN 1 
                ELSE 0
            END PTS
        FROM dc_match
    
        UNION ALL
    
        SELECT
            guest team_id,
            CASE
                WHEN guest_score IS NULL THEN 0
                ELSE 1
            END P,
            IF (guest_score > host_score,1,0) W,
            IF (guest_score = host_score,1,0) D,
            IF (guest_score < host_score,1,0) L,
            CASE 
                WHEN guest_score > host_score THEN 3
                WHEN guest_score = host_score THEN 1 
                ELSE 0
            END PTS
        FROM dc_match
        ) AS RNK
    LEFT JOIN dc_team t
        ON RNK.team_id = t.id
    GROUP by team_id
    ORDER BY points DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have two tables in MySQL, (and using PHP) which look similar to below:
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required
I have two mysql tables and i want to merge the results of these
i have two tables which form part of my login/register system: these are userlogin
I have two MySQL tables: User (with an auto-incremented pk id) and WaitingUser which
i have two mysql tables which are linked together and i need to show
I have two tables which are related as HABTM: Groups(id, name) Streams(id, Stream) Connecting
I have two MySQL tables, j and t, with a third normalising table, jt.

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.