我有一个包含四个表的小型MS Access数据库,并且正在使用SELECT COUNT查询,该查询涉及所有表。任何帮助将不胜感激。
数据库概述:
TABLE COLUMNS
Person Person_ID |....
Games Game_ID | Name |....
Played_Games Played_Games_ID | Game_ID |....
Participation Played_Games_ID | Person_ID |...
列出所有游戏以及每次游戏总共玩了多少次:
SELECT DISTINCT
Games.Name,
(SELECT COUNT(*) FROM Played_Games WHERE Played_Games.Game_ID = Games.Game_ID) AS TimesPlayed
FROM Games
ORDER BY Games.Name
这很有用,但我还要列出某一群人的每个游戏玩了多少次
我已经使用下面的代码(第4行)取得了一些进展,但我仍然遇到一个小问题:
最里面括号后面的Games.Game_ID引用不再链接到当前的Game_ID,就像在上面的代码中一样。它现在似乎未定义。当我在MS Access Query Designer中运行它进行测试时,它会要求我输入Games.Game_ID的值。如果我然后输入一些随机的Game_ID,代码就会“完美地”运行,但是正如你可能已经想到的那样,TimesPlayedByGroupMembers的值对于记录集中的每一行都是相同的,并且对于我输入的那个特定的Game_ID只有
SELECT DISTINCT
Games.Name,
(SELECT COUNT(*) FROM Played_Games WHERE Played_Games.Game_ID = Games.Game_ID) AS TimesPlayed,
(SELECT COUNT(*) FROM (SELECT DISTINCT Played_Games_ID FROM Participation WHERE Played_Games_ID IN (SELECT Played_Games_ID FROM Played_Games WHERE Game_ID = Games.Game_ID) AND Person_ID IN (26, 27, 28))) AS TimesPlayedByGroupMembers
FROM Games
ORDER BY Games.Name
我将尝试从内到外解释我的代码所做和/或应该做什么
最里面的括号列出了涉及当前游戏的Played_Games_ID,但这不起作用,因为Games.Game_ID没有链接到当前的Game_ID
中间括号(包括上面的括号)列出了涉及当前游戏和一个或多个所选人员的Played_Games_ID。这里列出的Person_ID只是一个例子,这部分代码正在运行
最外面的括号(包括上面所有)计算一个或多个选定人员每场比赛的次数
我真的坚持这一点,所以任何帮助将不胜感激
感谢您的时间
答案 0 :(得分:1)
您的第一个查询通常更常见:
SELECT
Games.Name, COUNT(Played_Games.Game_ID) AS TimesPlayed
FROM Games
INNER JOIN Played_Games ON Played_Games.Game_ID = Games.Game_ID
GROUP BY Games.Name
ORDER BY Games.Name
这可能与您第二次查询后的情况相近:
SELECT
Games.Name, COUNT(Participation.Played_Games_ID) AS TimesPlayedByGroupMembers
FROM Games
INNER JOIN Played_Games ON Played_Games.Game_ID = Games.Game_ID
INNER JOIN Participation ON Participation.Played_Games_ID = Played_Games.Played_Games_ID
WHERE Participation.Person_ID IN (26, 27, 28)
GROUP BY Games.Name
ORDER BY Games.Name
答案 1 :(得分:1)
我的Access语法有点生疏,我对你的表格结构并不是100%肯定,但我认为以下内容是可行的。
SELECT Games.Name,
TimesPlayed,
TimesPlayedByGroupMembers
FROM Games
INNER JOIN
( SELECT Game_ID,
COUNT(*) AS TimesPlayed,
SUM(IIF(ISNULL(Participation.Played_Games_ID),0,1)) AS TimesPlayedByGroupMembers
FROM Played_Games
LEFT JOIN
( SELECT Played_games_ID
FROM Participation
WHERE Person_ID IN (1, 2)
GROUP BY Played_games_ID
) AS Participation
ON Participation.Played_Games_ID = Played_Games.Played_Games_ID
GROUP BY Game_ID
) AS Played_Games
ON Played_Games.Game_ID = Games.Game_ID
ORDER BY Games.Name
附录:
要将尚未播放的游戏显示为0,请使用以下内容:
SELECT Games.Name,
IIF(ISNULL(TimesPlayed),0,TimesPlayed) AS TimesPlayed,
IIF(ISNULL(TimesPlayedByGroupMembers),0,TimesPlayedByGroupMembers) AS TimesPlayedByGroupMembers
FROM Games
LEFT JOIN
( SELECT Game_ID,
COUNT(*) AS TimesPlayed,
SUM(IIF(ISNULL(Participation.Played_Games_ID),0,1)) AS TimesPlayedByGroupMembers
FROM Played_Games
LEFT JOIN
( SELECT Played_games_ID
FROM Participation
WHERE Person_ID IN (1, 2)
GROUP BY Played_games_ID
) AS Participation
ON Participation.Played_Games_ID = Played_Games.Played_Games_ID
GROUP BY Game_ID
) AS Played_Games
ON Played_Games.Game_ID = Games.Game_ID
ORDER BY Games.Name