I am trying to generate a selection of table columns which is proving difficult – I am using phpmyadmin tool
I have a Players table and I have a teams table – My objective is to display each team in a single row however i want to add to that row every player_ign who obtains a tb_players.player_teamid = tb_teams.team_id.
additionally, for example we have 2 players with the same player_teamId, I want to have those clomun headers read as player1, player2, instead of having the same duplicated player_ign column we see in the expected result.
I have added an example of my expected result at the bottom
This is what I have tried – according to my logic, but sadly getting nowhere.
I understand that this doesnt make sense because the second SELECT returns 2 Rows and I need to have it as 2 columns with 1 row – but I am struggling with that logic
SELECT tb_teams.team_logo, tb_teams.team_name, tb_teams.team_abreviation, (SELECT
tb_players.player_ign FROM tb_players WHERE tb_players.player_id =tb_teams.team_id)
FROM tb_teams
INNER JOIN tb_players
ON tb_teams.team_id = tb_players.player_id
This is my Players table (tb_players)
This is my Teams Table (tb_teams)
This is my expected result which I am struggling to get to
2
Answers
There are no such things as dynamic columns. The number of columns should be equal for all records. So you cannot do this. You can get the players in one column, though.
Try this instead:
If you know maximum amount of players you can get in result, you can make columns for maximum result set and get desired list. All unused fields wil be NULL, so you will need to check if not NULL before usage.