mysql – Concatenate columns and return the relation by SELECT

Question:

Suppose I have the following tables and their relationships:

Tabela A
    Col 1
    Col 2
    Col 3

Tabela B
    Col 1
    Col 2
    Col 3

Tabela C
    Col 1
    Col 2
    Col 3

Tabela D
    Col 1
    Col 2
    Col 3


Relacionamentos:

A (col 1) ---> B (col 1)  ---> C(col 1) 

Tables A, B and C are listed through the 1st Column.

SELECT 

       Tabela A.Col 2,
       Tabela A.Col 3,
       Tabela B.Col 2,
       Tabela C.Col 3

       FROM Tabela A, Tabela B, Tabela C 

       WHERE Tabela A.Col 1 = Tabela B.Col 1    and     Tabela B.Col 1 = Tabela C.Col 1 

With the code above I would be able to partially fetch what I need. Now, what I want to do is to concatenate col 2 and col 3 from Table D to make the relation between B(col 1) — D(col 2 & col 3) and, later, pull col 1 from Table D . Is it possible to do that? If yes, how?

The Final Table would look like this:

Tabela All:
       A.Col 2,
       A.Col 3,
       B.Col 2,
       C.Col 3,
       D.Col 1

Answer:

Select simples:

    select * 
      from Tabela A
     inner join Tabela B on A.Col1 = B.Col1
     inner join Tabela C on A.Col1 = C.Col1
      left join Tabela D on B.Col1 = D.Col2+D.Col3

I used the left join for table D here because I understand that it is a composite key and may not bring necessary results for the join. Hope this helps.

Scroll to Top