mysql - 使用 != 而不是 < 时得到错误结果

标签 mysql sql database mysql-5.6

我是 SQL 初学者,我正在研究斯坦福 Lagunita 上的练习题,并遇到了一个奇怪的行为,当我使用 != 而不是 < 来表示不相等的值时,我会得到不同的结果。

问题是:

"For all cases where the same reviewer rated the same movie twice and gave it a higher rating the second time, return the reviewer's name and the title of the movie."

这是架构:

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

我的工作解决方案:

SELECT Reviewer.name, Movie.title
FROM Rating r1, Rating r2, Movie, Reviewer
WHERE Reviewer.rID = r1.rID and Reviewer.rID = r2.rID and 
      Movie.mID = r1.mID and Movie.mID = r2.mID and 
      r1.rID = r2.rID and r1.mID = r2.mID and 
      r1.ratingDate < r2.ratingDate and
      r2.stars > r1.stars

现在,如果您查看架构,您会发现对同一部电影有重复评论的评论者(仅评论者 rID:201 和 203)具有不同的 ratingDate,所以如果我更改 r1.ratingDate < r2.ratingDater1.ratingDate != r2.ratingDate ,结果还将包括 Brittany Harris (rID:203),这是不正确的。

谁能告诉我为什么会发生这种情况?

谢谢

最佳答案

r1.ratingDate < r2.ratingDate and r2.stars > r1.stars 

意思是:返回那些第二次评分并且给予更多星星的。

如果您不强制执行日期顺序 (!=),则该对的另一条记录(通过连接作为叉积生成)将导致返回 Brittany Harris。尝试将两者都设置为!=,您将看到不同时间不同评分的所有记录对。

关于mysql - 使用 != 而不是 < 时得到错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45134556/

相关文章:

mysql - MYSQL中查询的逆向

php - MySQL - 用户定义的变量可以被 cron 作业调用的所有脚本读取吗?

SQL: "Reverse"转置表

sql - 使用SQL条件检查时间间隔

对于没有记录的日期,MySQL 显示计数为 0

python - 在 Python 中生成用户 ID

php - 加入查询问题 - codeigniter

php - 触发两个表以获取主键(自动增量)到第三个链接表mysql

database - Elasticsearch - 多日志设计

javascript - 如何停止选择框以使用 select2 插件