Joining 3 tables same id different and same descriptions

时间:2021-10-22 20:21:11

I am attempting to join three tables together. Tables A, B, and C. The ID is the same for all three tables.

我试图加入三张桌子。表A,B和C.所有三个表的ID都相同。

Each table has a field descriptions. ID 1 with description Name in Table A should be ID 1 with description Name in table B. Likewise for Table C it should be ID1 with description Name.

每个表都有一个字段描述。具有描述的ID 1表A中的名称应该是ID 1,其中描述名称在表B中。对于表C,它应该是具有描述名称的ID1。

But some of the ID shows ID1 description Nameblank. I want to display SAME – A if it has the same value as B or C. And if all three are the same I do not want it displayed. For example:

但有些ID显示ID1描述Nameblank。我想显示SAME - 如果它与B或C具有相同的值。如果所有三个都相同,我不希望它显示。例如:

Table A

ID      Description

1       Name

2       Noname

3       Namea

Table B

ID      Description

1       Nameblank

2       Noname

3       Nameb

Table C

ID      Description

1       Name

2       Noname

3       Namec

Result

ID  A Description   B Description   C Description

1       |   Name  |  Namea        |   Nameblank

3       |       Nameb|  SAME – A      |    Namec

I haven't joined three tables before and I am confused. Thank you for any help.

我之前没有参加过三张桌子,我很困惑。感谢您的任何帮助。

2 个解决方案

#1


1  

SELECT 
    A.ID as 'ID',
    A.Description as 'A Description',
    B.Description as 'B Description',
    C.Description as 'C Description'
From
    TableA A
        INNER JOIN
    TableB B ON (A.ID = B.ID)
        INNER JOIN
    TableC C ON (A.ID = C.ID)

#2


0  

select TABLEA.ID, 
       TABLEA.Description as ADescription,

       CASE WHEN TABLEA.Description=TABLEB.Description THEN 'SAME - A'
                  ELSE TABLEB.Description
       END as BDescription,

       CASE WHEN TABLEA.Description=TABLEC.Description THEN 'SAME - A'
                  ELSE TABLEC.Description
       END as CDescription

from TABLEA
JOIN TABLEB on TABLEA.id=TABLEB.id
JOIN TABLEC on TABLEC.id=TABLEC.id

Where TABLEA.Description<>TABLEB.Description
      or 
      TABLEB.Description<>TABLEC.Description
ORDER BY TABLEA.id

#1


1  

SELECT 
    A.ID as 'ID',
    A.Description as 'A Description',
    B.Description as 'B Description',
    C.Description as 'C Description'
From
    TableA A
        INNER JOIN
    TableB B ON (A.ID = B.ID)
        INNER JOIN
    TableC C ON (A.ID = C.ID)

#2


0  

select TABLEA.ID, 
       TABLEA.Description as ADescription,

       CASE WHEN TABLEA.Description=TABLEB.Description THEN 'SAME - A'
                  ELSE TABLEB.Description
       END as BDescription,

       CASE WHEN TABLEA.Description=TABLEC.Description THEN 'SAME - A'
                  ELSE TABLEC.Description
       END as CDescription

from TABLEA
JOIN TABLEB on TABLEA.id=TABLEB.id
JOIN TABLEC on TABLEC.id=TABLEC.id

Where TABLEA.Description<>TABLEB.Description
      or 
      TABLEB.Description<>TABLEC.Description
ORDER BY TABLEA.id