我不是100%肯定如何说出这个问题。我尽可能多地搜索档案,但找不到我要找的东西。
我有三个数据库表。
tblSeason(ID,季)
tblPlayers(ID,名字,姓氏,显示名称,盘口,电流)
tblMatch(ID,MatchDate,季节,PLAYER1,Player2,Player1Score,Player2Score,优胜者)
我一直试图找出如何返回以下内容。
PLAYER1
Player2 - 2场比赛
Player3 - 1场比赛
Player2
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;
答案 0 :(得分:0)
您可以在查询中使用group by。
SELECT id, player, match
FROM tables
GROUP BY id, player, match
答案 1 :(得分: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;
如果您想了解有关玩家的其他信息(例如姓名),则需要加入其他表格。