简体   繁体   中英

Mysql table join 2 tables

i have two tables in mySQL:

Table 1: Club(ClubID=PK, club_name)

Table 2: League_table(tableID=PK, position, clubID=fk, games_played, points)

how would i join the two tables to give a query that displayed only

(position, club-name, 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).

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.

@Alexen: No need of left join in this case.

@Diegoe: one friendly advise, always use on in join, without it query goes slow down when you are working on big tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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