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)跟随游戏的所有评分。
我用过:
SELECT rating, name
FROM ratings, members
WHERE gameID = 2;
每当我使用此命令时,它会给我正确的评级值,但它会列出所有成员,即使该成员没有评价游戏。有人可以帮助弄清楚如何解决问题。
提前感谢所有
答案 0 :(得分:3)
您正在寻找JOIN
:
SELECT rating, name
FROM ratings r
INNER JOIN members m
ON r.MemberID = m.ID
WHERE gameID = 2;
答案 1 :(得分:0)
尝试:
SELECT rating, name
FROM ratings, members
WHERE gameID = 2 and ratings.memberID = members.id;