【问题标题】:JOIN not work with whereJOIN 不适用于 where
【发布时间】:2012-11-13 19:29:14
【问题描述】:

我有这个代码:

select
  B.plc_nomeConta, B.plc_classificador, B.plc_id,
  A.cap_idPlanoContasFin, SUM(A.cap_valorfatura) as Total     
from
  tbl_PlanoFinanceiro B
  left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin
   /*  where A.cap_idEmpresa like 2*/
group by
  B.plc_nomeConta,
  B.plc_classificador,
  B.plc_id,
  A.cap_idPlanoContasFin 

此代码返回 185 行,

(-) COFINS     10.01.01.01.004  330  330   971090,97
(-) ICMS       10.01.01.01.002  328  328   1378407,11
(-) IMPOSTOS   10.01.00.00.000  324  NULL  NULL
(-) IMPOSTOS   10.01.01.00.000  325  NULL  NULL
(-) IMPOSTOS   10.01.01.01.000  326  NULL  NULL
(-) ISS        10.01.01.01.001  327  327   1000960,59
(-) PIS        10.01.01.01.003  329  329   240600,27

但是当我取消注释 where /* where A.cap_idEmpresa like 2*/ 时,只返回出现 A.cap_idPlanoContasFin is not null、需要 B.plc_nomeConta, B.plc_classificador, B.plc_id 的行。

【问题讨论】:

  • 试试AND 而不是WHERE

标签: sql sql-server sql-server-2008


【解决方案1】:

您的WHERE 过滤器正在将LEFT OUTER JOIN 转换为INNER JOIN

本质上,您是在说“向我显示左侧的所有记录,并且仅显示右侧匹配且 cap_idEmpresa 值为 2 的记录”。

这意味着您只显示匹配的记录,即INNER JOIN——任何不匹配的记录在该字段中的值不能为2

要更正,您需要考虑 null:

WHERE (A.cap_idEmpresa like 2 OR A.cap_idEmpresa IS NULL)

或细化您的要求。

【讨论】:

  • 或者,您可以移动 A.cap_idEmpresa like 2 以成为联接的一部分。
  • @cadrell0 不过,向OUTER JOIN 添加值检查将改变JOIN 的逻辑。
  • @JNK 公平地说,我无法正确解释实际要求。我什至不能把它归咎于星期一。
  • @JNK 两种方式产生的结果集应该是相同的。如果您的意思是这样的话,我不确定哪个会有更有效的执行计划。
  • @JNK 我认为如果您进行直接交换,它们在语义上是不同的。您使用 OR 构建 WHERE 子句的方式使其在语义上等同于使条件部分成为连接。
【解决方案2】:

我已经用 JNK 和 cadrell0 解决了,使 A.Cap_idempresa like 2 成为 JOin 的一部分,感谢这里的代码

        alter proc Ntrimestre (@emp integer,@inicio datetime,@fim datetime) as
        select  B.plc_nomeConta, B.plc_classificador ,B.plc_id ,
        A.cap_idPlanoContasFin, SUM (A.cap_valorfatura) as Total
        from tbl_PlanoFinanceiro B
        left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin 
        and   A.cap_idEmpresa like @emp
        and A.cap_vencfatura <= convert(datetime,@fim,1) 
        and cap_vencfatura >= convert(datetime,@inicio,1) 
        group by B.plc_nomeConta, B.plc_classificador ,B.plc_id, A.cap_idPlanoContasFin
       order by B.plc_classificador 

与 where 的另一种方式:

       alter proc NtrimestreMzFSant (@inicio datetime,@fim datetime) as
       select  B.plc_nomeConta, B.plc_classificador ,B.plc_id ,
        A.cap_idPlanoContasFin, SUM (A.cap_valorfatura) as Total
       from tbl_PlanoFinanceiro B
       left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin  
        and A.cap_vencfatura <= convert(datetime,@fim,1) 
         and A.cap_vencfatura >= convert(datetime,@inicio,1) 
        where B.plc_tipo <> 'Sintética' and A.cap_idEmpresa =2 or A.cap_idEmpresa=2234 
        group by B.plc_nomeConta, B.plc_classificador ,B.plc_id, A.cap_idPlanoContasFin
        order by B.plc_classificador 

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多