I’ve got a table with these columns:
id | player1_name | player1_score | player2_name | player2_score | player3_name | player3_score | player4_name | player4_score | player5_name | player5_score
Given a single row, how do I get the highest playerX_score and the corresponding playerX_name?
I’ve tried using GREATEST(), but I can’t get the playerX_name.
As an aside, I think your table would be better designed as
id | name | score | position | teamid, wherepositiongoes from 1 to 5 andteamidserves to group everyone in the same team together. It would make this sort of query much easier (greatest-score-per-team with associated rows).However, here’s one way to do what you want with your current table: