自然连接与内部连接为 sqlite3 中的简单查询提供不同数量的行?

Natural join vs inner join giving different number of rows for a simple query in sqlite3?

我的 class 和谷歌搜索给我的印象是,内部联接和自然联接之间的唯一区别是自然联接仅 returns 您的列的一个副本'加入,而内部加入 returns 两者。所以我很惊讶地发现在下面的(简化的)示例中,自然连接 returns 2 行(这对我来说似乎是正确的),而内部连接 ​​returns 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(shared1 int, unshared2 text);
create table Rating(shared1 int, unshared3 int);

insert into Movie values(1, 'a');
insert into Movie values(2, 'b');
insert into Rating values(1,3);
insert into Rating values(1,3);

然后 sql 结果...

sqlite> select * from Movie natural join Rating;
1|a|3
1|a|3

/*seems correct*/

sqlite> select * from Movie inner join Rating;
1|a|1|3
1|a|1|3
2|b|1|3
2|b|1|3

/*seems strange and/or wrong*/

sqlite> select * from Movie inner join Rating on Movie.shared1 = Rating.shared1;
1|a|1|3
1|a|1|3

/*seems right*/

在此查询中:

select * from Movie inner join Rating

你使用 INNER JOIN 没有 ON 子句。

这相当于CROSS JOIN:

select * from Movie cross join Rating

通常 INNER JOIN 应该有一个 ON 子句,但在 SQLite 中它可以省略。

来自Determination of input data (FROM clause processing)

If the join-operator is "CROSS JOIN", "INNER JOIN", "JOIN" or a comma (",") and there is no ON or USING clause, then the result of the join is simply the cartesian product of the left and right-hand datasets.

参见demo