简体   繁体   English

如何将一列连接到另一个表中的所有列

[英]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.

相关问题 将一个表的两列连接到另一表的一列 - Join two columns from one table to one column from another 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 如何使用mysql将一个表中的所有列集成到另一个表的一列中 - how to integrate all columns from one table into one column of another table using mysql 将一个表中的多个列连接到另一个表中的单个列 - Join multiple columns from one table to single column from another table 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table Sequelize将一张表的两列连接到另一张表的同一列 - Sequelize join two columns of one table to the same column of another table SQL连接-一列用作另一表中两列的ID - SQL join - one column serving as ID for two columns in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM