左连接到子查询。想要查看不匹配的行

问题描述 投票:0回答:3

我正在加入子查询到外部查询,需要查看所有内容的结果,甚至是加入不匹配的结果。

我试图为外部查询创建一个临时表,然后为其中一个内部查询创建临时表,然后尝试加入该表。我试图插入外部查询,但它给了我很多行。我正在考虑使用具有匹配和不匹配的目标表的Merging语句。

Select 
col a  --- Can have Nulls or blanks in Table A
col b
col c
col d
col e
from table a
left join

( select
col a  This will never have blanks or nulls
col b
col c
col d
col e
from table)b

on a.cola =  b.cola  -- even though there might be nulls in table A show 
                        them. so A.ColA <> B.ColA and carry them to the next subquery
and a.colb = b.colb
and a.colc = b.colc
Left join

Whatever is NUll for ColA will go to the next subquery
(select
col a
col b
col c
col d
col e
from table)C

on a.colb =  b.colb
and a.colc = b.colc

当有匹配时它会显示,但我需要看到那些没有匹配的,所以我可以让它进入下一个子查询,看看是否匹配。

sql derived
3个回答
0
投票

如果没有关于你的问题和dbms的更多细节,我认为这会有所帮助,但我不确定:

Select a.*, ISNULL(b.col, c.col) --Will show you the column from b if it matched, or the column from c if there was no match on b
from Table a
left join Table b
on a.col = b.col... --More restrictive criteria here
left join Table c
on a.col = c.col...--Less restrictive criteria here

0
投票

从table1 t1中选择distinct t1.Id,t1.col1,t1.col2,t2.col1,在t1.Id!= t2.Id上连接table2 t2


0
投票

你似乎想在join中加入级联逻辑。你的问题在具体细节上有点模糊,但它看起来像这样:

Select coalesce(a.cola, c.cola) as cola,
       a.colb, a.colc,
       coalesce(b.cold, c.cold) as cold
       coalesce(b.cole, c.cole) as cole
from a left join
     b
     on a.cola = b.cola and
        a.colb = b.colb and
        a.colc = b.colc left join
     c
     on a.colA is null and  -- or is this b.colA
        a.colb = b.colb and
        a.colc = b.colc
© www.soinside.com 2019 - 2024. All rights reserved.