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

时间:2019-04-19 23:32:48

标签: sql derived

我正在将子查询连接到外部查询,并且需要查看所有结果,即使连接不匹配的结果也是如此。

我尝试为外部查询创建一个Temp表,为内部查询之一创建一个temp表,然后尝试加入该表。我试图对外部查询进行插入,但是它给了我很多行。我正在考虑将目标表与匹配项和不匹配项合并的语句。

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

当有一个匹配项时,它将显示出来,但是我需要查看那些不匹配的项,以便可以转到下一个子查询以查看是否存在匹配项。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

从表1 t1中选择不同的t1.Id,t1.col1,t1.col2,t2.col1在t1.Id上将table2 t2左连接起来!

答案 2 :(得分: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