0

I am attempting to combine two rows into a single column on mysql this is getting all two players in 1 single query and combine them using the team column, my table is below.

+-------------------+
| ID  player  Team  |
+-------------------+
| 1  John Doe Team1 |
| 2  Anna Doe Team1 |
+-------------------+

What I want to achieve is below.

+-----------------------------+
| ID  player1  player2  Team  |
+-----------------------------+
| 1  John Doe  Anna Doe Team1 |
+-----------------------------+

I am using PHP as my backend and doing a loop and pushing into a single array can do the job but I believe it can be attained using mysql query. any suggestion would be great.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Sarotobi
  • 707
  • 1
  • 9
  • 28

1 Answers1

2

Try this query:

select team, 
       min(player) player1, 
       max(player) player2 
from my_table
group by team

Since there are only two players per team, min and max will return the two.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69