SQL从单独的表中获取3个不同的值

时间:2014-12-10 18:43:58

标签: sql sql-server-2014

我有一个sql join,我试图从另一个表中同时获取3个值。以下是数据:( SiteID是Site,HomeId是home,VisitorId是Visitor)。我试过了

Select GameDate 
From Games 
INNER JOIN Schools ON Schools.SchoolId = Games.SiteId 
Where Games.GameId = '1'` 

但它只获得SiteId,而不是HomeId或VisitorId

Games table
GameId  GameDate    SiteId   HomeId   VisitorId
  1      1/5/15         2        2          1
  2      1/7/15         1        1          2
  3      1/8/15         1        1          2

Schools table
SchoolId    SchoolName
   1         SchoolA
   2         SchoolB

这是我想要的信息

Date       Site        Home      Visitor
1/5/15    SchoolB    SchoolB    SchoolA
1/7/15    SchoolA    SchoolA    SchoolB
1/8/15    SchoolA    SchoolA    SchoolB

5 个答案:

答案 0 :(得分:2)

您需要多次加入同一个表并使用表别名:

SELECT Games.GameDate AS Date,
    SiteSchool.SchoolName AS Site,
    HomeSchool.SchoolName AS Home,
    VisitorSchool.SchoolName AS Visitor
FROM Games
INNER JOIN Schools SiteSchool
    ON SiteSchool.SchoolId = Games.SiteId 
INNER JOIN Schools HomeSchool
    ON HomeSchool.SchoolId = Games.HomeId 
INNER JOIN Schools VisitorSchool
    ON VisitorSchool.SchoolId = Games.VisitorId 
WHERE Games.GameId = '1'

答案 1 :(得分:1)

您需要加入Schools表三次,每个键一个:

Select g.GameDate , s.SchoolName as Site, h.SchoolName as home, v.SchoolName as visitor
From Games g INNER JOIN
     Schools s
     ON s.SchoolId = g.SiteId INNER JOIN
     Schools h
     on h.SchoolId = g.HomeId INNER JOIN
     Schools v
     on v.SchoolId = g.VisitorId
Where Games.GameId = 1;

如果在from子句中多次使用相同的表,则需要使用表别名来区分它们。在这种情况下,别名是“s”代表网站,“h”代表家庭,“v”代表访问者。

答案 2 :(得分:1)

您必须为网站,家庭和访客学校名称进行三次联接。

这是一个工作示例

With Games As
(
          Select 1 GameId, '1/5/2015' GameDate, 2 SiteId, 2 HomeId, 1 VisitorId
    Union Select 2 GameId, '1/7/2015' GameDate, 1 SiteId, 1 HomeId, 2 VisitorId
    Union Select 3 GameId, '1/8/2015' GameDate, 1 SiteId, 1 HomeId, 2 VisitorId
),
Schools As
(
    Select 1 SchoolId, 'SchoolA' SchoolName
    Union Select 2 SchoolId, 'SchoolB' SchoolName
)
Select 
    GameDate, 
    SiteSchool.SchoolName Site,
    HomeSchool.SchoolName Home,
    VisitorSchool.SchoolName Visitor
From Games G
Inner Join Schools as SiteSchool 
    On G.SiteId = SiteSchool.SchoolId
Inner Join Schools as HomeSchool 
    On G.SiteId = HomeSchool.SchoolId
Inner Join Schools as VisitorSchool 
    On G.SiteId = VisitorSchool.SchoolId

答案 3 :(得分:1)

@HLGEM是正确的。

Select g.GameDate
     , s1.SchoolName as Site
     , s2.SchoolName as Home
     , s3.SchoolName as Visitor
From Games g INNER JOIN 
Schools s1 ON Schools.SchoolId = Games.SiteId INNER JOIN
Schools s2 ON Schools.SchoolId = Games.HomeId INNER JOIN
Schools s3 ON Schools.SchoolId = Games.VisitorId

答案 4 :(得分:1)

您需要INNER JOIN Schools 3次。

Select GameDate, S1.SchoolName, S2.SchoolName, S3.SchoolName
From Games 
INNER JOIN Schools S1 ON S1.SchoolId = Games.SiteId 
INNER JOIN Schools S2 ON S2.SchoolId = Games.HomeId
INNER JOIN Schools S3 ON S3.SchoolId = Games.VisitorId
Where Games.GameId = '1'