SQLite自然联接坏了吗? [英] SQLite natural join broken?

查看:67
本文介绍了SQLite自然联接坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是逐渐了解NATURAL JOIN,而SQLite却不符合我的预期.

I am just getting to know NATURAL JOIN and SQLite is not behaning as I expect.

SELECT * FROM r1 NATURAL JOIN (r2 NATURAL JOIN r3);

SELECT * FROM (r1 NATURAL JOIN r2) NATURAL JOIN r3;

产生相同(正确)的结果.

produce the same (correct) results.

但是,如果我省略括号,如下所示:

However if I omit the parentheses as in:

SELECT * FROM r1 NATURAL JOIN r2 NATURAL JOIN r3;

我看到r1和r2正确连接,但是r3根本没有连接到结果,而是形成了r1 NATURAL JOIN r2,r3的笛卡尔积.

I see that r1 and r2 are joined correctly however r3 is not joined to the result at all, instead the cartesian product of r1 NATURAL JOIN r2, r3 is formed.

第一个联接结果的属性名称是否存在问题,或者我误解了SQL?

Is there an issue with the attribute names of the first join result, or am I misinterpreting SQL?

推荐答案

我自己不使用该数据库,但根据此文档,SQLite中的所有联接均基于左右表的笛卡尔积.

I don't use that database myself, but according to this documentation, all joins in SQLite are based on the cartesian product of the left and right tables.

NATURAL 联接使用公共列名作为键"来组合两个表.使用括号强制在处理外部联接之前构建派生表".如果没有括号,它将不会看到"公用的列名,因此第二个 NATURAL 关键字将被忽略.

NATURAL joins use common column names as "keys" to combined two tables. Using parentheses forces a "derived table" to be built before the outer join is processed. Without parentheses, it does not "see" the common column names, so the second NATURAL keyword is ignored.

一些建议:最好理解 NATURAL JOIN 的工作原理,以防您在代码中看到它,但不要自己使用它.这是一个危险的"构造,几乎没有价值.只是我的意见.

A little advice: it's good to understand the mechanics of how a NATURAL JOIN works in case you ever see it in code, but do not use it yourself. It is a "dangerous" construct with little value. Just my opinion.

这篇关于SQLite自然联接坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆