我正在尝试学习 ansi-92 SQL 标准,但我似乎并没有完全理解它(我对 ansi-89 标准以及一般的数据库也是新手)。
在我的例子中,我有三个表 Kingdom -< family -< species(生物学分类)。
为什么会发生这种情况?
比如说一个生物学家,发现了一个新物种,但他没有把它归入一个界或科,创建一个没有物种的新科,并且不确定它应该属于哪个界,等等。
这是一个 fiddle (见最后一个查询):http://sqlfiddle.com/#!4/015d1/3
我想做一个查询来检索我的每个王国、每个物种,但不是那些没有物种的家庭,所以我做了这个。
select *
from reino r
left join (
familia f
right join especie e
on f.fnombre = e.efamilia
and f.freino = e.ereino
) on r.rnombre = f.freino
and r.rnombre = e.ereino;
我认为这会做的是:
我错了吗?这不应该让我看到那些没有被分类的物种吗?如果我进行内部查询,它会带来我想要的。我在分组的地方有问题吗?
最佳答案
如果这有帮助:
Relationally speaking, [
OUTER JOIN
is] a kind of shotgun marriage: It forces tables into a kind of union—yes, I do mean union, not join—even when the tables in question fail to conform to the usual requirements for union. It does this, in effect, by padding one or both of the tables with nulls before doing the union, thereby making them conform to those usual requirements after all. But there's no reason why that padding shouldn't be done with proper values instead of nulls, as in this example:
SELECT SNO , PNO
FROM SP
UNION
SELECT SNO , 'nil' AS PNO
FROM S
WHERE SNO NOT IN ( SELECT SNO FROM SP )
以上相当于:
SELECT SNO , COALESCE ( PNO , 'nil' ) AS PNO
FROM S NATURAL LEFT OUTER JOIN SP
来源:
SQL and Relational Theory: How to Write Accurate SQL Code By C. J. Date
关于sql - 我误解了联接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901403/