'Tuple Variables',它们是什么以及为什么要使用它们? (包括代码)

'Tuple Variables', what are they and why use them? (code included)

select c1.customer_name, c1.customer_street
from customer as c1, customer as c2

where c1.customer_street = c2.customer_street

and c1.customer_name <> c2.customer_name;

所以这段代码是从我在网上找到的练习中摘录的。从查看它我可以收集到的所有信息是,它似乎正在创建两个由来自同一位置的数据组成的独立对象,看起来它正在比较这两个对象并返回结果。

我是从编程的角度来处理它的,因为我对 SQL 的了解相当基础。我只是不明白这个查询中实际发生了什么。能详细解释一下吗?

提前致谢。

简答:该查询将为您提供在您的 table.

中有邻居的所有客户

逐行:

select c1.customer_name, c1.customer_street

最终,您将从客户那里获得姓名和街道列表 table。

from customer as c1, customer as c2

您将使用此 table 两次,交叉引用自身(见下文)

where c1.customer_street = c2.customer_street
and c1.customer_name <> c2.customer_name;

将 c1 中的每个客户与 c2 中的所有客户进行匹配,确保街道匹配且名称不同(<> 是 "not equal to" 的运算符)。

一个简单的例子:

Name        Street:
John Doe    Baker
Mary Sue    Baker
Zach Smith  Dover

JD/Baker    JD/Baker    streets match, name match.  CULL.
JD/Baker    MS/Baker    streets match, name mismatch.  Keep.
JD/Baker    ZS/Dover    no street match.  CULL.
MS/Baker    JD/Baker    streets match, names don't.  Keep.
MS/Baker    MS/Baker    streets match, name match.  CULL.
MS/Baker    ZS/Dover    no street match.  CULL.
ZS/Dover    JD/Baker    no street match.  CULL.
ZS/Dover    MS/Baker    no street match.  CULL.
ZS/Dover    ZS/Dover    streets match, name match.  CULL.

Records kept:

JD/Baker
MS/Baker