为什么LEFT JOIN查询中此列始终为NULL?

时间:2014-08-06 11:25:46

标签: sql sql-server

我遇到了一些麻烦。我一直在编写一个SQL查询,从2个表中获取5列。在简单连接中,名为CustomerCode的列正确显示,但在下面的查询中,它显示为NULL。实际上,我想从两个表中选择左连接。

这是我的疑问:

  select fff.*,CustomerCode,SUBSTRING(( SELECT DISTINCT
                                                RIGHT('0000'
                                                      + LTRIM(RTRIM(CustomerCode)),
                                                      4)
                                                       + '-'
                                        FROM    TatEstelam.dbo.CustomerInfo
                                        WHERE   ltrim(rtrim(CodeMelli))  <> '' and CodeMelli <> '0000000000' AND CODEMELLI  <> '0'  AND  Nationalcode = fff.CodeMelli
                                      FOR
                                        XML PATH('')
                                      ), 0,
                                      LEN(( SELECT DISTINCT
                                                    RIGHT('0000'
                                                      + LTRIM(RTRIM(CustomerCode)),
                                                      4) 
                                                      + '-'
                                            FROM    TatEstelam.dbo.CustomerInfo
                                            WHERE  ltrim(rtrim(CodeMelli))  <> '' and CodeMelli <> '0000000000' AND CODEMELLI  <> '0'  AND  Nationalcode = fff.CodeMelli 
                                          FOR
                                            XML PATH('')
                                          ))) 
                                          AS customerNumber from ( SELECT  NameOutput AS Fullname ,
                shobehNew AS BRANCHNO ,
                ShenaseMelli AS Nationalcode ,
                [CodeMelli],

                CreateDate AS RegisterDate ,


                N'ساها حقوقی' as type
        FROM    TatEstelam.dbo.tblCodeMRequest_0  where CreateDate BETWEEN  '920101' AND '930501'  and CusType = 6) as fff
       LEFT JOIN (select DISTINCT Nationalcode,customercode,NationalID from TatEstelam.dbo.CustomerInfo where InqueryType <> '01' ) ci ON fff.Nationalcode = ci.NationalID and ltrim(rtrim(CodeMelli))  <> '' and CodeMelli <> '0000000000' AND CODEMELLI  <> '0' 
        ORDER BY CODEmELLI

但在此查询中,客户代码将显示:

     select * from TatEstelam.dbo.CustomerInfo c right join TatEstelam.dbo.tblCodeMRequest_0 m
         on c.Nationalcode = m.ShenaseMelli

0 个答案:

没有答案