通过avg(x)在SQL中选择值

时间:2013-01-25 19:35:19

标签: sql sqlite sql-update average

我改变了这个问题,因为我做了另一个错误的部分。我的目标是更新电影的年份信息,其中包含了> = 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 个答案:

答案 0 :(得分:1)

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

UPDATE tableName 
SET fieldName = fieldName + 25

如果您需要定位特定值,请添加到您的条件上方示例的末尾,例如:

WHERE fieldName < 1000

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

希望这有帮助! ;)

编辑:不再与OP更改的问题相关...请仅为了清晰而更改问题,不要完全改变它的含义! AGRHHH !!!

答案 1 :(得分: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

答案 2 :(得分:0)

Update TargetTable
set targetColumn = somevalue
where PrimaryKey = somevalue

如果您没有primaryKey,则需要找到一些方法来唯一标识该行,否则您可能会冒更新整个表的风险!

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

编辑:

Update TargetTable
set targetColumn = TargetColumn + somevalue

这将为所有行添加25年

答案 3 :(得分:0)

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

但就像你说你想要特定值一样,你必须在第WHERE项中指明它们:

UPDATE mytable
 SET year = year + 25
 WHERE condition

condition可能是

WHERE mypk > 100