我不是 100% 确定如何表达这个问题。我尽可能多地搜索档案,但找不到我要找的东西。
我有三个数据库表。
tblSeason(ID,季节)
tblPlayers(ID,名字,姓氏,显示名,障碍,当前)
tblMatch(ID,MatchDate,Season,Player1,Player2,Player1Score,Player2Score,Winner)
我一直在尝试找出如何返回如下内容。
播放器1
Player2 -- 2 场对阵
Player3 -- 1 场对阵
播放器2
Player1 -- 2 场对阵
Player3 -- 3 场对阵
谢谢
SELECT p4.displayname AS [Opponent], count(*) AS [Games Against]
FROM ((SELECT player1,player2 FROM tblMatch)
UNION ALL SELECT player2, player1 FROM tblMatch) m
INNER JOIN tblPlayers p4 ON m.Player2=p4.id)
WHERE player1=(SELECT p3.id FROM tblPlayers p3 WHERE p3.displayname=?)
GROUP BY player1, player2;
我正在使用 Visual Studio 并插入参数来一次为 1 个玩家执行此查询。
以下是在不显示玩家姓名的情况下工作。
SELECT m.Player2 AS [Opponent], count(*) AS [Games Against]
FROM (SELECT player1,player2 FROM tblMatch
UNION ALL SELECT player2, player1 FROM tblMatch) m
WHERE player1=1
GROUP BY player2;
感谢您的帮助,为我指明了正确的方向。我最终得到了我想要的结果。
SELECT p4.displayname AS [Opponent], count(*) AS [Games Against]
FROM ((SELECT player1, player2
FROM(tblMatch)
UNION ALL
SELECT player2, player1
FROM tblMatch) m
INNER JOIN tblPlayers p4 ON m.Player2=p4.id)
WHERE player1=1
GROUP BY p4.displayname;