【问题标题】:How muliti table join in stored procedure?如何在存储过程中加入多表?
【发布时间】:2012-09-03 11:39:38
【问题描述】:

我想要多表连接。它显示 1 条数据/显示 2 条记录,但实际上是 1 条数据/1 条记录。

select acl.Person_ID as 'CODE'
,pnr.FullName as 'FullName'
,case acl.persontype when 'normal' then 'normal' end as 'Type'
From tbl_aculog acl left join tbl_PerNR pnr On acl.Person_ID=pnr.Person_ID

union

select acl.Person_ID as 'CODE'
,ps.FullName as 'FullName'
,case acl.persontype when 'blacklist' then 'blacklist' end as 'Type'
From tbl_aculog acl left join tbl_Person ps On acl.Person_ID=ps.NPerson_ID

结果:

Person_ID |全名 |类型
00010132 |斯汀|普通的
00010132 |空 |空值
00000579 |普洛姆 |普通的
00000579 |空 |空值
00001081 |华生 |普通的
00001081 |空 |空值
5211080 |索比特 |黑名单
5211080 |空 |空

**字段 Person_ID & FullName & Type 为 NULL VALUE。

我想要结果:

Person_ID |全名 |类型
00010132 |斯汀|普通的
00000579 |普洛姆 |普通的
00001081 |华生 |普通的
5211080 |索比特 |黑名单

非常感谢您的宝贵时间:D

【问题讨论】:

    标签: sql-server stored-procedures join left-join inner-join


    【解决方案1】:

    要从结果中删除 NULLs,您需要指定 WHERE acl.persontype IS NOT NULL 以忽略空的“persontype”。

    select acl.Person_ID as 'CODE'
    ,pnr.FullName as 'FullName'
    ,case acl.persontype when 'normal' then 'normal' end as 'Type'
    From tbl_aculog acl left join tbl_PerNR pnr On acl.Person_ID=pnr.Person_ID
    WHERE acl.persontype IS NOT NULL
    
    union
    
    select acl.Person_ID as 'CODE'
    ,ps.FullName as 'FullName'
    ,case acl.persontype when 'blacklist' then 'blacklist' end as 'Type'
    From tbl_aculog acl left join tbl_Person ps On acl.Person_ID=ps.NPerson_ID
    WHERE acl.persontype IS NOT NULL
    

    编辑 1

    使用INNER JOIN 将删除NULLS

    select acl.Person_ID as 'CODE'
    ,pnr.FullName as 'FullName'
    ,case acl.persontype when 'normal' then 'normal' end as 'Type'
    From tbl_aculog acl inner join tbl_PerNR pnr On acl.Person_ID=pnr.Person_ID
    
    union
    
    select acl.Person_ID as 'CODE'
    ,ps.FullName as 'FullName'
    ,case acl.persontype when 'blacklist' then 'blacklist' end as 'Type'
    From tbl_aculog acl inner join tbl_Person ps On acl.Person_ID=ps.NPerson_ID
    

    【讨论】:

      【解决方案2】:

      没有结果

      更多:

      Result:
      
      Person_ID |    FullName |    Type
      00010132 |  Stin|         normal
      00010132 |  NULL |        NULL
      00000579 |  Plom |        normal
      00000579 |  NULL |        NULL
      00001081 |  Watson |          normal
      00001081 |  NULL |        NULL
      5211080 |   SOPIT |       blacklist
      5211080 |   NULL |        NULL
      NULL | NULL |        NULL
      NULL | NULL |        NULL
      
      I want Result:
      
      Person_ID |    FullName |    Type
      00010132 |  Stin|         normal
      00000579 |  Plom |        normal
      00001081 |  Watson |          normal
      5211080 |   SOPIT |       blacklist
      NULL | NULL |        NULL
      NULL | NULL |        NULL
      

      对不起,数据相关。

      到“njk”代码结果没有 NULL VALUE 但我想在不重复值时显示 NULL VALUE。

      非常感谢您再次光临。 :)

      【讨论】:

        猜你喜欢
        • 2010-10-29
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多