联接表的SQL别名
我有这样一个问题:联接表的SQL别名,sql,sql-server,join,alias,outer-join,Sql,Sql Server,Join,Alias,Outer Join,我有这样一个问题: select a1.name, b1.info from (select name, id, status from table1 a) as a1 right outer join (select id, info from table2 b) as b1 on (a1.id = b1.id) select z1.name, z1.info from ((select name, id, stat
select a1.name, b1.info
from (select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)
select z1.name, z1.info
from ((select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)) as z1
where z1.status = 1
select a1.name, b1.info from
(
select name, id
from table1 a
where a.status = 1
) as a1
right outer join
(
select id, info
from table2 b
) as b1 on (a1.id=b1.id)
我只想包含a1.status=1的所有内容,因为我使用的是外部联接,所以我不能只向表1添加一个where约束,因为表2中我想要排除的所有信息仍然存在,只是没有名称。我是这样想的:
select a1.name, b1.info
from (select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)
select z1.name, z1.info
from ((select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)) as z1
where z1.status = 1
select a1.name, b1.info from
(
select name, id
from table1 a
where a.status = 1
) as a1
right outer join
(
select id, info
from table2 b
) as b1 on (a1.id=b1.id)
但我认为这是不合法的
编辑:
正如下面所描述的,外部连接实际上对我要做的事情没有意义。例如,如果我想要表2中的所有数据where status=表1中的1,包括表1中根本不存在对应ID的所有数据。因此,我需要表2中所有数据的外部联接,但仍然希望排除那些状态为1的条目
相当于:
select z1.name, z1.info
from ((select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)) as z1
where z1.status != 1
将where子句添加到子查询,如下所示:
select a1.name, b1.info
from (select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)
select z1.name, z1.info
from ((select name, id, status
from table1 a) as a1
right outer join (select id, info
from table2 b) as b1 on (a1.id = b1.id)) as z1
where z1.status = 1
select a1.name, b1.info from
(
select name, id
from table1 a
where a.status = 1
) as a1
right outer join
(
select id, info
from table2 b
) as b1 on (a1.id=b1.id)
编辑:
对于第二个场景:
select a1.name, b1.info from
(select name, id, status from table1 a) as a1
right outer join
(select id, info from table2 b) as b1 on (a1.id=b1.id)
EXCEPT
select a1.name, b1.info from
(select name, id, status from table1 a WHERE status<>1) as a1
right outer join
(select id, info from table2 b) as b1 on (a1.id=b1.id)
这应该是可行的,因为不管怎样,您都将获得所有table2数据
编辑2:
好的,要从表2中获取所有信息,除了表1中有状态ID的地方,即使表1中没有条目,也需要使用EXCEPT函数,它基本上会从较大的数据集中排除一个子集
SELECT a1.Name, b1.Info
FROM table2 b1
JOIN table2 a1 ON b1.id= a1.id AND a1.status = 1
右外部联接与左外部联接的作用完全相同,只是切换了表。您可以对联接进行筛选,但它仍将包含来自初始表的数据。请参见上文。因为这是一个外部连接,所以它不起作用。它实际上不会排除表2中的所有内容。是否还有其他原因需要在这个表上进行右外部连接,而不是左内部连接?在大多数情况下,几乎不需要右连接。不需要。只要是外部连接,就不需要右连接。将其作为左外部连接并解决问题,除非您需要表2中的孤立记录。最初我希望表2中的所有记录,直到我意识到在这种情况下没有意义。不过,我觉得可能会有这样一种情况,在执行外部联接之后,我希望检查约束,我认为没有任何解决方案可以解决这个问题。也许这种情况根本不存在。我想我意识到了我的问题。。。外部连接对我正在做的事情没有意义。如果状态需要为1,这意味着记录必须存在于表1中,因此我应该使用内部联接。@Linceum-作为对您一些评论的回应-我认为您不了解联接的工作原理。左外部联接显示第一个表中的所有记录,然后显示第二个表中的任何匹配记录,如果不匹配,则显示NULL。右外部联接的作用正好相反-表2中的所有记录,仅在表1中匹配。内部联接只显示两个表中条件匹配/记录的位置。实际上,我非常了解联接的作用。我最初使用正确的外部联接从表2中获取所有数据,以确保无论表1中是否存在相应的ID,我都获得了所有数据。从那以后,我决定只需要那些活跃的人的数据,这由表1的状态表示。我试图进行此修改,但没有意识到,如果表1中必须存在具有活动状态的对应记录,则外部联接将变得毫无意义。@Linceum-Super,如果我遇到这种情况,我不是有意居高临下或侮辱他人。连接是SQL最常被误解的方面之一,仅此而已:不用担心,我的问题在逻辑上没有什么意义,所以我知道你怎么会认为这是我自己对这个概念的误解。不过,我认为我修改后的问题是有道理的。