[英]how to join one column to all columns from another table
I have two tables, players and lineup table with all players in the team我有两张桌子,球员和阵容表与团队中的所有球员
the lineup table have this columns阵容表有这个列
`match_id`, `goalkeeper`, `center_back`, `center_back_2`, `right_outside_back`, `left_outside_back`, `defensive_center_midfielder`, `center_midfielder`, `attacking_center_midfielder`, `right_winger`, `left_winger`, `center_forward`
and all of the columns have the ID of the one player in the players table并且所有列都具有玩家表中一名玩家的 ID
How to join all the columns here to the player table ?如何将这里的所有列加入到玩家表中?
something in line with this:与此相符的东西:
select match_id,
gk.player_name as goalkeeper,
c_back.player_name as center_back
from matches
left join players as gk on gk.id = matches.goalkeeper
left join players as c_back on c_back.id = matches.center_back
...
where match_id = x
basically a lot of left_joins.基本上很多left_joins。
If you redesing your database model and add table that will have columns: match_id, position_id, player_id things will be much simpler and database engine will be grateful to you.如果您重新设计数据库模型并添加包含以下列的表:match_id、position_id、player_id 事情会简单得多,数据库引擎将感谢您。
then your SQL would look much simpler:那么你的 SQL 看起来会简单得多:
select positions.* from position_definition as positions
left join (select mp.*, players.player_name from match_players as mp left join players on mp.player_id = players.id where match_id = x)
as team on team.position_id = positions.id
(something like that, did not check for accuracy here) there is additional table added holding a list of all positions in a match. (类似的东西,这里没有检查准确性)还添加了一个额外的表格,其中包含一场比赛中所有位置的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.