2

我有一行,其中两个不同的团队用他们的 ID 和名称替换。我想在下拉列表中显示它们。为此,我需要以一种在另一个之上的格式显示它们。这是我的查询和图像是我得到的当前结果..

SELECT Match_Schedule.Match_Serno as 'Id', FirstHomeTeam.Serno as 'HomeTeamID', FirstHomeTeam.Team_Name as 'HomeTeam',SecondHomeTeam.Serno as 'AwayTeamID',SecondHomeTeam.Team_Name as 'AwayTeam'
 FROM Match_Schedule 
 INNER JOIN  Team_Detail AS FirstHomeTeam ON Match_Schedule.HomeTeam = FirstHomeTeam.Serno
 INNER JOIN Team_Detail AS SecondHomeTeam ON Match_Schedule.AwayTeam = SecondHomeTeam.Serno
 where Match_Serno=436

我得到的结果是在此处输入图像描述

但我想要的结果是。在此处输入图像描述

谢谢你。

4

3 回答 3

1

你通过UNPIVOT在你的Match_Schedule桌子上做一个然后对桌子做一个JOINTeam_Detail获得团队名称来实现这一点。

SELECT U.TeamID, TD.Team_Name
FROM Match_Schedule
UNPIVOT (TeamID FOR TeamName IN (HomeTeam, AwayTeam)) U
JOIN Team_Detail TD ON U.TeamID = TD.Serno
WHERE Match_Serno = 436
于 2013-09-12T00:11:14.180 回答
0

您需要将数据按摩成两行,然后使用如下外部查询查询这些行:

SELECT Id, TeamID
FROM (
    SELECT Serno as ID, Team_Name as TeamID, 'H' as TeamType 
    FROM Team_Detail 
    WHERE Serno = (SELECT HomeTeam FROM Match_Schedule WHERE Match_Serno = 436)
    UNION ALL
    SELECT Serno as ID, Team_Name as TeamID, 'A' as TeamType 
    FROM Team_Detail 
    WHERE Serno = (SELECT AwayTeam FROM Match_Schedule WHERE Match_Serno = 436)
)
ORDER BY TeamType DESC
于 2013-09-12T00:08:20.523 回答
0

不需要花哨的 UNPIVOT 或子选择语句,以下将为您提供所需的内容:

SELECT MS.Match_Serno AS 'Id', T.Serno AS 'TeamID', T.Team_Name AS 'TeamName'
FROM Match_Schedule MS
JOIN Team_Detail T
    ON MS.HomeTeam = T.Serno
    OR MS.AwayTeam = T.Serno
WHERE MS.Match_Serno = 436

如果需要,您可以省略第一列,我只是将其包括在内以供参考。

于 2013-09-12T01:07:24.010 回答