简体   繁体   English

MySQL表联接2个表

[英]Mysql table join 2 tables

i have two tables in mySQL: 我在MySQL中有两个表:

Table 1: Club(ClubID=PK, club_name) 表1:俱乐部(ClubID = PK,club_name)

Table 2: League_table(tableID=PK, position, clubID=fk, games_played, points) 表2:League_table(tableID = PK,排名,clubID = fk,games_played,得分)

how would i join the two tables to give a query that displayed only 我将如何联接两个表以给出仅显示的查询

(position, club-name, games_played) (位置,俱乐部名称,games_played)

Simple join: 简单加入:

select l.position, c.club_name, l.games_played
from club c, league_table l 
where l.clubid=c.clubid

You are looking for a left join. 您正在寻找左联接。 ClubID is the foreign key (the column "connecting" the two tables). ClubID是外键(“连接”两个表的列)。

select position, club_name, games_played
from league_table
left join club on club.ClubId = league_table.clubID

select a.club_name, b.position, b.games_played from club as a join league_table as b on a.clubid=b.clubid Thats what you want. 从a.clubid = b.clubid上选择a.club_name,b.position,b.games_played from club作为加入League_table的b,这就是您想要的。

@Alexen: No need of left join in this case. @Alexen:在这种情况下,不需要左联接

@Diegoe: one friendly advise, always use on in join, without it query goes slow down when you are working on big tables. @Diegoe:一个友好的建议,始终在联接中使用,如果不使用它,则在处理大表时查询会变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM