I’m attempting to run an SQL query to display the results of a set of fights in a competition.
Fight details are stored in the fields fighter1, fighter2 and winner.
What I need to do is put together a list of all fights, with each fight appearing twice in the list – once with, for example
Competitor:fighter1, Opponent:fighter2, Winner:fighter1
and also, elsewhere in the list
Competitor:fighter2, Opponent:fighter1, Winner:fighter1
I’ve been using an sql union to pull the details from each fight twice, and trying to assign different aliases to the fighter1 and fighter2 columns to swap them around, but i’m not even sure if this is possible.
My query is like this (with unnecessary stuff from other tables omitted)
SELECT wp_ema_fights.fighter1 AS competitor,
wp_ema_fights.fighter2 AS opponent,
wp_ema_fights.winner AS winner
FROM wp_ema_fights
UNION ALL
SELECT wp_ema_fights.fighter1 AS opponent,
wp_ema_fights.fighter2 AS competitor,
wp_ema_fights.winner AS winner
FROM wp_ema_fights
ORDER BY competitor
This query is displaying each fight twice, but like this:
Competitor:fighter1, Opponent:fighter2, Winner:fighter1
Competitor:fighter1, Opponent:fighter2, Winner:fighter1
Has anyone got any idea how I can swap the two around and order the list by whichever fighter has been assigned as ‘competitor’ each time??
Thanks in advance.
Try: