这可能很简单,但我不确定如何去做。 我有2桌比赛和球员
Players table
-------------
PlayerID int
PlayerName varchar(20)
TeamID int
TeamName varchar(10)
Match table
-----------
MatchID int
Team1 varchar(20)
Team2 varchar(20)
MatchDate date
例如,对于特定比赛,我想列出两支球队的所有球员名称。
说28/03/16 Liverpool vs Arsenal
,如何从玩家表中获取玩家名称?
你能告诉我的一切都会有所帮助。
答案 0 :(得分:0)
不是将团队名称作为team1和team2存储在匹配表中,而是存储相应的团队ID。
编辑:每个匹配都应该是唯一匹配ID,因此不需要日期过滤。
这可以帮助您使用查询检索记录:
select * from players a where exists(select 1 from match b where
b.MATCH_ID= :match_id and (a.team_id=b.team1 OR
a.team_id =b.team2))
答案 1 :(得分:0)
从match
表中获取行。如果你不知道team1是利物浦还是阿森纳,反之亦然,对于team2来说
SELECT m.matchid
FROM match m
WHERE m.date = TO_DATE('2016-03-28','YYYY-MM-DD')
AND m.team1 IN ('Liverpool','Arsenal')
AND m.team2 IN ('Liverpool','Arsenal')
AND m.team1 <> m.team2
接下来,制作行的两个副本......
SELECT m.matchid
FROM (SELECT 1 AS i UNION ALL SELECT 2) d
CROSS
JOIN match m
WHERE m.date = TO_DATE('2016-03-28','YYYY-MM-DD')
AND m.team1 IN ('Liverpool','Arsenal')
AND m.team2 IN ('Liverpool','Arsenal')
AND m.team1 <> m.team2
执行连接到player表,看起来我们使用teamname列来匹配。
SELECT CASE WHEN d.i = 1 THEN m.team1 ELSE m.team2 END AS teamname
, p.playername
FROM (SELECT 1 AS i UNION ALL SELECT 2) d
JOIN match m
ON m.date = TO_DATE('2016-03-28','YYYY-MM-DD')
AND m.team1 IN ('Liverpool','Arsenal')
AND m.team2 IN ('Liverpool','Arsenal')
AND m.team1 <> m.team2
LEFT
JOIN players p
ON p.teamname IN (m.team1, m.team2)
ORDER BY d.i, p.playerid