0

我改变了这个问题,因为我做错了另一部分。我的目标是更新平均> = 4的电影的年份信息。但我无法通过他们的平均来列出它们:(

/* 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');

这些是桌子。

4

4 回答 4

1

按照您在问题中编写示例的方式,您将在字段的字符串值的末尾添加一个字符串“25”。它也仅适用于字符串(文本)字段,而不适用于数字字段。您的示例会将一个值为“1000”的字段更改为“100025”。您可能想要做的是添加到数值。只需删除示例中的单引号即可。

UPDATE tableName 
SET fieldName = fieldName + 25

如果您需要定位特定值,请在您的标准上方的示例末尾添加,例如:

WHERE fieldName < 1000

并且只会对符合条件的记录进行更改。

希望这可以帮助!;)

编辑:不再与 OP 改变的问题相关......请只为了清楚起见而改变问题,不要完全改变它的含义!啊啊啊!!!

于 2013-01-25T19:43:05.510 回答
1

对于一个特定的电影 ID,您可以通过以下查询获得平均评分:

SELECT avg(stars)
FROM Rating
WHERE mID = ?

要更新具有特定平均评分的所有电影,请使用上述作为子查询:

UPDATE Movie
SET year = year + 25
WHERE (SELECT avg(stars)
       FROM Rating
       WHERE Rating.mID = Movie.mID) >= 4
于 2013-01-25T22:02:21.793 回答
0
Update TargetTable
set targetColumn = somevalue
where PrimaryKey = somevalue

如果您没有主键,您将需要找到某种方法来唯一标识行,否则您可能会更新整个表!

Msdn 页面涵盖了 Update 语句可以做什么和不能做什么。

编辑:

Update TargetTable
set targetColumn = TargetColumn + somevalue

这将使所有行增加 25 年

于 2013-01-25T19:36:33.673 回答
0

year您的代码将更新所有行。

但是就像你说你想要特定的值,你必须在子句中指定它们WHERE

UPDATE mytable
 SET year = year + 25
 WHERE condition

condition可能是主

WHERE mypk > 100
于 2013-01-25T19:42:49.767 回答