首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOIN not work

JOIN not work
EN

Stack Overflow用户
提问于 2012-11-14 03:29:14
回答 2查看 74关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
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行,

代码语言:javascript
复制
(-) 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,In need ever B.plc_nomeConta, B.plc_classificador, B.plc_id出现的行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-14 03:31:50

您的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)

或者改进你的需求。

票数 4
EN

Stack Overflow用户

发布于 2012-11-15 01:24:50

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

代码语言:javascript
复制
        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的另一种方式:

代码语言:javascript
复制
       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 

感谢每一个人的帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13367385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档