2013-11-20 55 views
0
CREATE TABLE members 
(
name  varchar(40), 
ID   char(6)   PRIMARY KEY 
); 

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

CREATE TABLE ratings 
(
memberID char(6)   REFERENCES members(ID), 
rating  SMALLINT CHECK(rating >= 1 AND rating <= 8), 
gameID  integer   REFERENCES games(ID), 
     PRIMARY KEY (memberID, gameID) 
); 

我試圖找到所有評級給予的遊戲ID爲(2)以下每個成員誰評價它。查找每個成員在SQL中給它評分的評分

我用:

SELECT rating, name 
    FROM ratings, members 
    WHERE gameID = 2; 

每當我用這個命令,它給了我正確的評價值,但它列出了所有的成員,即使成員未評定遊戲。有人可以幫助弄清楚如何解決問題。

感謝所有提前

回答

0

嘗試:

SELECT rating, name 
FROM ratings, members 
WHERE gameID = 2 and ratings.memberID = members.id; 
+0

感謝,它完美 – user2821471

3

您正在尋找JOIN

SELECT rating, name 
FROM ratings r 
    INNER JOIN members m 
     ON r.MemberID = m.ID 
WHERE gameID = 2;