SQL Query打印4次/建议

时间:2016-03-18 20:18:11

标签: sql oracle join duplicates inner-join

当我使用以下查询查询此表时,我正在重复行。我不确定为什么会这样,有人可以帮忙解释一下吗?

当我在查询数据时,当预期结果只有1行时,我得到4个重复的行。

查询是:

SELECT d.DIRECTOR_FNAME, d.Director_lname, s.studio_name
FROM DIRECTOR d, STUDIO s, FILM f, CASTING c
WHERE s.STUDIO_ID = f.STUDIO_ID
AND f.FILM_ID = c.FILM_ID
AND d.DIRECTOR_ID = c.DIRECTOR_ID
AND f.FILM_TITLE = 'The Wolf Of Wall Street';

这是表格,我可能不需要把整张桌子放进去,但现在已经完成了。

drop table casting;
drop table film;
drop table studio;
drop table actor;
drop table director;

CREATE TABLE studio(
studio_ID NUMBER NOT NULL,
studio_Name VARCHAR2(30),
PRIMARY KEY(studio_ID));

CREATE TABLE film(
film_ID NUMBER NOT NULL,
studio_ID NUMBER NOT NULL,
genre VARCHAR2(30),
genre_ID NUMBER(1),
film_Len NUMBER(3),
film_Title VARCHAR2(30) NOT NULL,
year_Released NUMBER NOT NULL,
PRIMARY KEY(film_ID),
FOREIGN KEY (studio_ID) REFERENCES studio);

CREATE TABLE director(
director_ID NUMBER NOT NULL,
director_fname VARCHAR2(30),
director_lname VARCHAR2(30),
PRIMARY KEY(director_ID));

CREATE TABLE actor(
actor_ID NUMBER NOT NULL,
actor_fname VARCHAR2(15),
actor_lname VARCHAR2(15),
PRIMARY KEY(actor_ID));

CREATE TABLE casting(
film_ID NUMBER NOT NULL,
actor_ID NUMBER NOT NULL,
director_ID NUMBER NOT NULL,
PRIMARY KEY(film_ID, actor_ID, director_ID),
FOREIGN KEY(director_ID) REFERENCES director(director_ID),
FOREIGN KEY(film_ID) REFERENCES film(film_ID),
FOREIGN KEY(actor_ID) REFERENCES actor(actor_ID));

INSERT INTO studio (studio_ID, studio_Name) VALUES (1, 'Paramount');
INSERT INTO studio (studio_ID, studio_Name) VALUES (2, 'Warner Bros');
INSERT INTO studio (studio_ID, studio_Name) VALUES (3, 'Film4');
INSERT INTO studio (studio_ID, studio_Name) VALUES (4, 'Working Title Films');

INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (1, 1, 'Comedy', 1, 180, 'The Wolf Of Wall Street', 2013);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (2, 2, 'Romance', 2, 143, 'The Great Gatsby', 2013);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (3, 3, 'Science Fiction', 3, 103, 'Never Let Me Go', 2008);
INSERT INTO film (film_ID, studio_ID, genre, genre_ID, film_Len, film_Title, year_Released) VALUES (4, 4, 'Romance', 4, 127, 'Pride and Prejudice', 2005);

INSERT INTO director (director_ID, director_fname, director_lname) VALUES (1, 'Martin', 'Scorcese');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (2, 'Baz', 'Luhrmann');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (3, 'Mark', 'Romanek');
INSERT INTO director (director_ID, director_fname, director_lname) VALUES (4, 'Joe', 'Wright');

INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (1, 'Matthew', 'McConnaughy');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (2, 'Leonardo', 'DiCaprio');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (3, 'Margot', 'Robbie');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (4, 'Joanna', 'Lumley');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (5, 'Carey', 'Mulligan');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (6, 'Tobey', 'Maguire');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (7, 'Joel', 'Edgerton');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (8, 'Keira', 'Knightly');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (9, 'Andrew', 'Garfield');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (10, 'Sally', 'Hawkins');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (11, 'Judi', 'Dench');
INSERT INTO actor (actor_ID, actor_fname, actor_lname) VALUES (12, 'Matthew', 'Macfadyen');

INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (1, 1, 1);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (1, 2, 1);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (1, 3, 1);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (1, 4, 1);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (2, 2, 2);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (2, 5, 2);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (2, 6, 2);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (2, 7, 2);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (3, 5, 3);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (3, 8, 3);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (3, 9, 3);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (3, 10, 3);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (4, 5, 4);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (4, 8, 4);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (4, 11, 4);
INSERT INTO casting (film_ID, actor_ID, director_ID) VALUES (4, 12, 4);

3 个答案:

答案 0 :(得分:1)

你应该问的第一个问题是"我在寻找什么样的结果?"。您的查询将返回导演名字,导演姓氏和工作室名称,其中电影片名是"华尔街的狼",对于属于该电影的每个演员。你得到4条记录,因为在这种情况下,你得到每个CastI的记录,其中FilmId = 1。包括ActorId列,你会看到我在说什么。希望有所帮助......如果不是,我会更早地学习。

答案 1 :(得分:0)

您缺少连接,而不是AND,请按照以下示例:

SELECT d.DIRECTOR_FNAME, d.Director_lname, s.studio_name
FROM   DIRECTOR d
INNER  JOIN STUDIO  s ON s.STUDIO_ID = f.STUDIO_ID
INNER  JOIN CASTING c ON d.DIRECTOR_ID = c.DIRECTOR_ID
INNER  JOIN FILM    f ON f.FILM_ID = c.FILM_ID
WHERE f.FILM_TITLE = 'The Wolf Of Wall Street';

答案 2 :(得分:0)

为了防止重复查询结果,只需在SELECT后添加DISTINCT。这就是它应该是这样的:

SELECT DISTINCT d.DIRECTOR_FNAME, d.Director_lname, s.studio_name
FROM DIRECTOR d, STUDIO s, FILM f, CASTING c
WHERE s.STUDIO_ID = f.STUDIO_ID
AND f.FILM_ID = c.FILM_ID
AND d.DIRECTOR_ID = c.DIRECTOR_ID
AND f.FILM_TITLE = 'The Wolf Of Wall Street';

同样,在表casting中,您没有正确的主键,请添加CastingId之类的内容。以同样的方式,尽量不要手写Id,而是自动增加它们。