-2

我不是 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;
4

2 回答 2

0

您的查询的基本形式是:

select player1, player2, count(*)
from tblMatch m
group by player1, player2;

但是,您实际上希望以两种方式报告游戏。为此,您可以使用union all

select player1, player2, count(*)
from (select player1, player2
      from tblMatch
      union all
      select player2, player1
      from tblMatch
     ) m
group by player1, player2;

如果您想了解有关玩家的更多信息(例如他们的姓名),那么您需要加入额外的表格。

于 2013-08-25T02:10:04.647 回答
0

您可以在查询中使用分组依据。

SELECT id, player, match
FROM tables
GROUP BY id, player, match
于 2013-08-25T01:45:41.897 回答