如何在同一列中使用具有2个条件的内部联接

时间:2015-09-29 11:39:06

标签: sql sql-server sql-server-2008 inner-join

我试图对几张桌子进行INNER JOIN,但有一定的条件:

我有CONTACTS表:

code                contact_type                  name               email
----------------------------------------------------------------------------
1                       P                       AAAAAAAA            SSSS@DDD.COM
2                       P                        BBBBBBB             SDFSDF@DDD.COM
3                       P                         CCCCC              SDSDF@DD.COM
1                       C                        AAAABBB             DDDD@DDD.COM
2                       C                        BBBBCCC             DSDF@DD.COM
3                       C                        CCCCDDD             ASSAD@DD.COM

PROVIDERS表:

code                               name
----------------------------------------------------
1                                  SIEMENS
2                                  FUJITSU
3                                  ASUS

和CLIENTS表:

CODE                                     NAME
---------------------------------------------------------
1                                       SMITH
2                                       PETER
3                                       MICHAEL

我想从联系人中选择所有行,但当CONTACT_TYPE为C时,此代码会显示相应的CLIENT.name,并且当CONTACT_TYPE为P时,会显示相应的PROVIDER.NAME },

我试试:

SELECT code,contact_type,con.name,con.EMAIL 
FROM contacts con 
  inner join CLIENTS cli 
    on cli.codcli=con.CODE 
   and con.CONTACT_TYPE='C'
  inner join provIDERS p 
     on con.CODE=p.codpro 
    and con.CONTACT_TYPE='P' 

但是没有用,

我也尝试: [SQL Fiddle Demo]

我希望看到这样:

PROVIDER/CLIENT NAME            CONTACT_TYPE                      EMAIL
---------------------------------------------------------------------------
SIEMENS                             P                          SSSS@DDD.COM
FUJITSU                             P                          SDFSDF@DDD.COM 
ASUS                                P                          SDSDF@DD.COM
SMITH                               C                          DDDD@DDD.COM
PETER                               C                          DSDF@DD.COM
MICHAEL                             C                          ASSAD@DD.COM

[SQL Fiddle Demo]

4 个答案:

答案 0 :(得分:2)

您可以尝试使用union来执行此操作:

SELECT  p.Name AS [Provider/Client Name] ,
        c.Contact_Type ,
        c.Email
FROM    contacts c
        INNER JOIN providers p
        ON c.code = p.code
WHERE  c.Contact_Type = 'P'
UNION
SELECT  cl.Name AS [Provider/Client Name] ,
        c.Contact_Type ,
        c.Email
FROM    contacts c
        INNER JOIN client cl
        ON  cl.code = p.code
WHERE   c.Contact_Type = 'C'

或用例陈述:

SELECT  CASE
        WHEN c.Contact_Type = 'P' THEN 
            (SELECT p.Name FROM providers p WHERE p.code = c.code)
        ELSE 
            (SELECT cl.Name FROM client cl WHERE cl.code = c.code) END
        AS [Provider/Client Name] ,
        c.Contact_Type ,
        c.Email
FROM    contacts c

答案 1 :(得分:1)

尝试使用left join

SELECT code, contact_type, con.name, con.EMAIL 
FROM contacts con left join
     CLIENTS cli
     on cli.codcli =c on.CODE and
        con.CONTACT_TYPE = 'C' left join
     providers p 
     on con.CODE = p.codpro and
        con.CONTACT_TYPE = 'P' ;

inner join要求每一行同时匹配两个表。鉴于条件,这显然是不可能的。因此,请使用left join

对于您的实际结果:

SELECT coalesce(con.name, p.name) as name, cli.contact_type, cli.email 
FROM contacts con left join
     CLIENTS cli
     on cli.codcli = con.CODE and
        con.CONTACT_TYPE = 'C' left join
     providers p 
     on con.CODE = p.codpro and
        con.CONTACT_TYPE = 'P' ;

答案 2 :(得分:1)

我建议你使用这个查询:

SELECT 
    CASE 
        WHEN CONTACT_TYPE = 'C' THEN
            (SELECT Name FROM CLIENTS c WHERE c.Code = co.Code)
        WHEN CONTACT_TYPE = 'P' THEN
            (SELECT Name FROM PROVIDERS p WHERE p.Code = co.Code)
    END AS [PROVIDER/CLIENT NAME],
    CONTACT_TYPE, EMAIL
FROM
    CONTACTS co;

[SQL Fiddle Demo]

答案 3 :(得分:1)

你想要的是这样的:

SELECT CASE WHEN C.ContactType = 'P' THEN P.Name,
        WHEN C.ContactType = 'C' THEN C.Name,
        ELSE 'Unknown' END AS ProviderClientName,
        C.ContactType,
        C.Name
FROM CONTACTS C WITH(NOLOCK)
LEFT JOIN CLIENTS CL WITH(NOLOCK) ON C.Code = CL.Code
LEFT JOIN PROVIDERS P WITH(NOLOCK) ON C.Code = P.Code

但请检查您的数据,因为您示例中的Contacts和Clients表之间的连接不会返回匹配项。