查询多个条件

时间:2016-03-20 10:57:35

标签: sql oracle inner-join

我需要一个查询才能找到一部同时包含 Keira Knightly Carey Mulligan 的电影。

我将如何找到在同一部电影中扮演角色的这两位演员。我自己尝试了它,它导致重复行。我想因为我可能没有使用内连接。有人可以伸出援助之手向我展示正确的查询方式吗?

这是表格:

drop table film_director;
drop table film_actor;
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 film_actor(
film_ID NUMBER NOT NULL,
actor_ID NUMBER NOT NULL,
PRIMARY KEY(film_ID, actor_ID),
FOREIGN KEY(film_ID) REFERENCES film(film_ID),
FOREIGN KEY(actor_ID) REFERENCES actor(actor_ID));

CREATE TABLE film_director(
film_ID NUMBER NOT NULL,
director_ID NUMBER NOT NULL,
PRIMARY KEY(film_ID, director_ID),
FOREIGN KEY(film_ID) REFERENCES film(film_ID),
FOREIGN KEY(director_ID) REFERENCES director(director_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 film_actor (film_ID, actor_ID) VALUES (1, 1);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 2);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 3);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (1, 4);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 2);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 6);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (2, 7);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 8);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 9);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (3, 10);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 5);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 8);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 11);
INSERT INTO film_actor (film_ID, actor_ID) VALUES (4, 12);

INSERT INTO film_director (film_ID, director_ID) VALUES (1,1);
INSERT INTO film_director (film_ID, director_ID) VALUES (2,2);
INSERT INTO film_director (film_ID, director_ID) VALUES (3,3);
INSERT INTO film_director (film_ID, director_ID) VALUES (4,4);

3 个答案:

答案 0 :(得分:3)

您的查询实际上返回了正确的结果?我对此表示怀疑.. 您可以使用EXISTS():

SELECT f.film_title AS "Mulligan and Knightly"
FROM Film f
INNER JOIN film_actor x
 ON(f.film_id = x.film_id)
INNER JOIN actor a
 ON(x.actor_id = a.actor_id)
WHERE a.actor_id = 8
      AND EXISTS(select 1 FROM film_actor s
                where s.film_id = x.film_id
                  and s.actor_id = 5)

这也可以通过简单的group by和having子句来完成:

SELECT f.film_title AS "Mulligan and Knightly"
FROM Film f
INNER JOIN film_actor x
 ON(x.film_id = f.film_id)
WHERE x.actor_id in(5,8)
GROUP BY f.film_title 
HAVING COUNT(*) = 2

答案 1 :(得分:1)

也许这样的事情会做到吗?

string summaryFilter = ...;
Dt.Defaultview.RowFilter = summaryFilter + 
    " OR ID like '%" + SearchTextBox.Text +"%'";

答案 2 :(得分:1)

您可以将表格连接到自己,如下所示。在这个例子中,我假设你知道演员ID。如果你想保持按名称搜索,那么你也要再次加入演员两次。

--keira == 8.  carey == 5.
select f1.film_Title as Both_In
from
  film_actor fa1
  inner join film f1 on f1.film_ID = fa1.film_ID
  inner join film_actor fa2 on fa2.actor_ID = 5 and fa2.film_ID = fa1.film_ID
where fa1.actor_ID = 8