1

私はいくつかのテーブルを持っています:

/* Delete the tables if they already exist */
drop table if exists Movie;
drop table if exists Reviewer;
drop table if exists Rating;

/* Create the schema for our tables */
create table Movie(mID int, title text, year int, director text);
create table Reviewer(rID int, name text);
create table Rating(rID int, mID int, stars int, ratingDate date);

/* Populate the tables with our data */
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming');
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas');
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise');
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg');
insert into Movie values(105, 'Titanic', 1997, 'James Cameron');
insert into Movie values(106, 'Snow White', 1937, null);
insert into Movie values(107, 'Avatar', 2009, 'James Cameron');
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg');

insert into Reviewer values(201, 'Sarah Martinez');
insert into Reviewer values(202, 'Daniel Lewis');
insert into Reviewer values(203, 'Brittany Harris');
insert into Reviewer values(204, 'Mike Anderson');
insert into Reviewer values(205, 'Chris Jackson');
insert into Reviewer values(206, 'Elizabeth Thomas');
insert into Reviewer values(207, 'James Cameron');
insert into Reviewer values(208, 'Ashley White');

insert into Rating values(201, 101, 2, '2011-01-22');
insert into Rating values(201, 101, 4, '2011-01-27');
insert into Rating values(202, 106, 4, null);
insert into Rating values(203, 103, 2, '2011-01-20');
insert into Rating values(203, 108, 4, '2011-01-12');
insert into Rating values(203, 108, 2, '2011-01-30');
insert into Rating values(204, 101, 3, '2011-01-09');
insert into Rating values(205, 103, 3, '2011-01-27');
insert into Rating values(205, 104, 2, '2011-01-22');
insert into Rating values(205, 108, 4, null);
insert into Rating values(206, 107, 3, '2011-01-15');
insert into Rating values(206, 106, 5, '2011-01-19');
insert into Rating values(207, 107, 5, '2011-01-20');
insert into Rating values(208, 104, 3, '2011-01-02');

映画の公開年が 1970 年以前または 2000 年以降で、評価が 4 つ星未満の行を評価テーブルから削除するクエリを作成する必要があります。まず、年 > 2000 としましょう。もう 1 つは年 < 1970 です。これら 2 つと 3 つ目の評価条件を結合するにはどうすればよいですか?

4

2 に答える 2

3

以下は、評価が 4 つ星未満の年の範囲 [1970-2000] 以外の映画の評価を削除します。

DELETE FROM Rating
WHERE EXISTS (
    SELECT 1 
    FROM Movie 
    WHERE Movie.mID = Rating.mID 
    AND (year < 1970 OR year > 2000)
)
AND stars < 4

SQL フィドルの例

于 2013-01-26T12:31:21.530 に答える
0
DELETE r
FROM Rating r
INNER JOIN Movie m
    ON r.mID = m.mID
WHERE (m.year < 1979 OR m.year > 2000)
AND r.rating < 4

ほとんどのRDMSで実行する必要がありますが、SQLiteでは実行できません。

于 2013-01-26T12:33:36.457 に答える