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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:11:01+00:00 2026-06-05T17:11:01+00:00

This is a continuation of this question from yesterday . Here are my three

  • 0

This is a continuation of this question from yesterday.

Here are my three tables:

Fighters Table

fighter_id |  name
-----------------------
1          | John
2          | Steve
3          | Bill
4          | Bobby

Events Table

event_id  |  event_name  |  event_date
-------------------------------------------
1          | MMA         |  01/01/2010
2          | Cool        |  02/20/2010
3          | Yeaa!       |  04/15/2010

Fights Table

fight_id  |  fighter_a  |  fighter_b  |  winner  |  method  |  event
-----------------------------------------------------------------------
1         | 1           |  2          |  1       | Sub      | 1
2         | 4           |  1          |  4       | KO       | 2
3         | 1           |  3          |  NULL    | Draw     | 3

Result trying to get

result  |  opponent  |  method  |  event  |  date        
----------------------------------------------------------
Draw    | Bill       |  Draw    |  Yeaa!  |  04/15/2010  
Loss    | Bobby      |  KO      |  Cool   |  02/20/2010 
Win     | Steve      |  Sub     |  MMA    |  01/01/2010

So in the fights table, fighter_a, fighter_b, and winner are integers that correspond to fighter_id in the Fighters table.

I’m basically on a page retrieving data based on fighter_id ($fighter_id).

I’m trying to create rows with each fight of that fighter that includes his opponent’s name, result (win, loss, draw, or nc), method, event_name, and event_date. The challenge is that a winner can be in either fighter_a or fighter_b. It’s not always in the same column. I appreciate any help I can get.

select 
    fight_id,
    CASE
      WHEN winner is not null and winner=fighter_id then 'win'
      WHEN winner is not null and winner<>fighter_id then 'loss'
      WHEN winner is null and method='Draw' then 'draw'
      WHEN winner is null and method = 'No Contest' then 'no contest'
      ELSE ''
    END as match_result,
    participant.name 'participant',
    opponent.name 'opponent'
FROM fights
    INNER JOIN fighters as participant on participant.fighter_id = fights.fighter_a
    INNER JOIN fighters as oppoent on opponent.fighter_id = fights.fighter_b
WHERE 
    fighter_a=$fighter_id OR fighter_b=$fighter_id
ORDER BY
    event_date DESC
  • 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-05T17:11:03+00:00Added an answer on June 5, 2026 at 5:11 pm

    Use a subselect with conditionals to switch the fighter_id you’re looking for to column_a if it’s in column_b, that way, it simplifies your operations and joins in the outer query:

    SELECT
        (
            CASE
                WHEN a.winner = a.f_a THEN 'Win'
                WHEN a.winner = a.f_b THEN 'Loss'
                WHEN a.winner IS NULL THEN a.method
            END
        ) AS result,
        b.name AS opponent,
        a.method AS method,
        c.event_name AS event,
        c.event_date AS date
    FROM
        (
            SELECT 
                IF(fighter_b = $fighter_id, fighter_b, fighter_a) AS f_a,
                IF(fighter_b = $fighter_id, fighter_a, fighter_b) AS f_b,
                winner,
                method,
                event
            FROM 
                fights
            WHERE
                $fighter_id IN (fighter_a, fighter_b)
        ) a
    INNER JOIN
        fighters b ON a.f_b = b.fighter_id
    INNER JOIN
        events c ON a.event = c.event_id
    ORDER BY
        c.event_date DESC
    

    Also, if the winner field is null, then just echo the method field. That way, when you want to add yet another type of method where winner is null to your system, you don’t have to keep tacking on more conditional checks to your CASE statement.

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

Sidebar

Related Questions

This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary (It's
Ok - this is in continuation from my earlier question about sending an email
This is a continuation of the question here: JBoss - does app have to
This question is a continuation of my previous question here zend models architecture (big
This is a continuation from a previous stackoverflow question. I've renamed some variables so
In a continuation from my question here I tried using the solution provided in
This question is in continuation to my previous post located here . Since there
yesterday i asked one question and got the answer from our friend here, and
A continuation from this question I need a SQL statement that returns the number
Ok this is a continuation from this question: How to make a simple Hello

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.