将SELECT语句与COUNT一起使用

时间:2013-11-22 05:42:55

标签: sql

CREATE TABLE hardware
(
name        varchar(10),
ID      serial              PRIMARY KEY    NOT NULL
);

CREATE TABLE games
(
name        varchar(100),
ID      serial                  PRIMARY KEY
);

CREATE TABLE gameVersion
(
gameID      integer         REFERENCES games(ID),
version     integer,
ID      char(10)        PRIMARY KEY     NOT NULL
);

CREATE TABLE versionHardware
(
hardwareID      integer         REFERENCES hardware(ID),
versionID       char(10)        REFERENCES gameVersion(ID),
ID      serial          PRIMARY KEY
);

CREATE TABLE cheatCodes
(
ID      serial          PRIMARY KEY     NOT NULL,
code        varchar(60),
gameID      integer         REFERENCES games(ID)
);

如何找到ID为(5)的硬件可以播放的游戏中记录的作弊码数量。

我试过

SELECT COUNT(*)
    FROM cheatCodes, games, gameVersion,hardware, versionHardware
    WHERE cheatCodes.GameId = games.ID
    AND   gameVersion.gameID = games.ID
    AND   versionHardware.versionID = gameVersion.ID
    AND   hardware.ID = 5;

但它没有给我正确答案。

2 个答案:

答案 0 :(得分:0)

请尝试使用以下查询。 缺少硬件表的链接。

SELECT COUNT(*)
    FROM cheatCodes, games, gameVersion,hardware, versionHardware
    WHERE cheatCodes.GameId = games.ID
    AND   gameVersion.gameID = games.ID
    AND   versionHardware.versionID = gameVersion.ID
    AND   hardware.id = versionHardware.hardwareID      
    AND   hardware.ID = 5;

答案 1 :(得分:0)

尝试:

SELECT COUNT(DISTINCT cheatCodes.GameId) as CheatCodeCount
FROM cheatCodes, games, gameVersion,hardware, versionHardware
WHERE cheatCodes.GameId = games.ID
AND   gameVersion.gameID = games.ID
AND   versionHardware.versionID = gameVersion.ID
AND   versionHardware.hardwareID = hardware.id 
AND   hardware.ID = 5;