选择多个外键的语句

时间:2017-05-09 11:06:42

标签: mysql

我有这些表格:

Livestream
----------
id_livestream int primary key,
name_match varchar(255),
date_match varchar(255),
time_match varchar(255),
league_type int,
tour varchar(255),
stadium int,
id_team1 int,
id_team2 int,
live_video varchar(255),

Team
------
id_team int primary key,
name_team varchar(255),
image_team varchar(255)

League
------
id_league int primary key,
name_league varchar(255)

staduim
-------
id_stadium int primary key,
name_stadium varchar(255)

我正在使用此SQL查询从表中获取数据,如下所示:

select id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
    live_video
 from Livestream,League,staduim,IsLive
   where Livestream.league_type=League.id_league and Livestream.stadium=staduim.id_stadium

我得到的是什么:

id_livestream|name_match|date_match|time_match|name_league|tour|stadium

     65       BarcaMatch  9/5/2017   20:45       League1    22  CampNou 

此查询进展顺利,但我不知道如何将此查询更改为:

从Team中选择名称team,image_team 两者 id_team1,来自Livestream的id_team2

更新:我搜索并发现此查询以获得我想要的但我无法将其添加到我的第一个查询中:

SELECT
    lm.id_team1,
    t1.name_team AS name_team_1,
    t1.image_team AS image_team_1,
    lm.id_team2,
    t2.name_team AS name_team_2,
    t2.image_team AS image_team_2
FROM Livestream lm
INNER JOIN Team t1
    ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
    ON lm.id_team2 = t2.id_team

更新2:

enter image description here 我需要帮助

1 个答案:

答案 0 :(得分:1)

只需在explizite join格式中重写整个查询:

SELECT
    lm.id_team1,
    t1.name_team AS name_team_1,
    t1.image_team AS image_team_1,
    lm.id_team2,
    t2.name_team AS name_team_2,
    t2.image_team AS image_team_2,
    id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video

FROM Livestream lm
INNER JOIN Team t1
    ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
    ON lm.id_team2 = t2.id_team
JOIN League on lm.league_type=League.league 
JOIN staduim on lm.stadium=staduim.id_stadium