繁体   English   中英

连接从同一表创建的两个子查询

[英]Joining two subqueries that were created from the same table

我有一个带有列的表TABLE1:

key, person, date, type, trait1, trait2, trait3, trait4, trait 5

现在说我从TABLE1创建两个临时表:

create temp1 as
select key, person, date, trait1, trait2
from TABLE1
where trait1=trait2

create temp2 as
select key, person, type, trait3, trait4
from TABLE1
where type='A' and trait3=trait4

如果我要像这样加入表:

create table TABLE2 as
select A.key, A.person, A.date, B.type, A.trait1, A.trait2, B.trait3, B.trait4
from temp1 A, temp2 B
where A.key = B.key and A.person=B.person

我将得到与从原始TABLE1创建表2相同的结果:

create table TABLE2 as
select key, person, date, type, trait1, trait2, trait3, trait4
from TABLE 1
where trait1=trait2 and type='A' and trait3=trait4

从逻辑上讲,这似乎会产生相同的结果,对吗? 这个例子主要是为了说明我的问题:如果您根据每个表的不同条件将一个表过滤为两个子表,然后像这样将它们联接,结果是否会像您只是为两个条件过滤原始表一样?

如果key / person唯一地定义每一行,那么结果将非常接近。 联接将合并与第一个条件匹配的行本身,但前提是它与第二个条件匹配。

他们什么时候会有所不同? 那就是当条件A.key = B.key and A.person = B.person无法获得匹配的行时。 keypersonNULL时,就会发生这种情况。

当然,如果key / person 不能唯一地标识给定的行,则这两种方法是不同的,这是微不足道的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM