gpt4 book ai didi

postgresql - 内连接sql的结合性和交换性

转载 作者:行者123 更新时间:2023-11-29 13:58:21 25 4
gpt4 key购买 nike

假设我有这个模式。

Boats
_____
bid
bname

Reserves
________
sid
bid
date

Sailors
_______
sid
sname

我知道内部联接应该是关联的和交换的,但我真的不明白为什么。

给定查询:

SELECT sname, bname
FROM (Sailors NATURAL INNER JOIN Boats) NATURAL INNER JOIN Reserves

我认为这应该返回 null,因为 Sailors 和 Boats 没有公共(public)字段,而:

SELECT sname, bname
FROM (Sailors NATURAL INNER JOIN Reserves) NATURAL INNER JOIN Boats

应该返回水手的名字和他们预定的船的名字。

请告诉我为什么内部联接应该同时具有交换性和关联性。

谢谢!

最佳答案

SELECT sname, bname
FROM (Sailors NATURAL INNER JOIN Boats) NATURAL INNER JOIN Reserves

I am thinking that this should return null since Sailors and Boats have no common fields, . . .

在 PostgreSQL 中 natural join两个没有公共(public)列的表之间的行为类似于交叉连接。

create table boats (
bid integer primary key,
bname varchar(15)
);

create table sailors (
sid integer primary key,
sname varchar(15)
);

insert into boats values (1, 'One'), (2, 'Two'), (3, 'Three');
insert into sailors values (1, 'One'), (2, 'Two'), (3, 'Three');

SELECT sname, bname
FROM (Sailors NATURAL INNER JOIN Boats);
sname    bname--One      OneOne      TwoOne      ThreeTwo      OneTwo      TwoTwo      ThreeThree    OneThree    TwoThree    Three

关于postgresql - 内连接sql的结合性和交换性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26814955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com