MySQL - Select comparing fields from the same table

0

I know it must be simple, but I'm having trouble solving it. I have a table called "enterprise". In this table, I have 4 specific fields that store different ID's from other records in that same table.

Example of the enterprise table.     The values of the fields do not follow this same order, it was just to exemplify:

 id      relacionado1   relacionado2    relacionado3    relacionado4
 10      15             16              17              18
 15      10             16              17              18
 16      15             10              17              18
 17      15             16              10              18

I need to make a select, showing for example all records with id equal to the fields of related tables1, related2, related3, related4.

How can I do this? I can not do it at all. Thank you in advance!

    
asked by anonymous 12.07.2017 / 15:42

1 answer

0

Use Inner Join to perform the comparison:

link

For example:

select 
   EP.COLUNA_ID AS ID,
   EP.COLUNA_2,
   EP.COLUNA_3,
   R1.COLUNA_N,
   R2.COLUNA_N
from empreendimento ep
INNER JOIN RELACIONADO1 R1 
  ON(R1.ID = EP.ID)
INNER JOIN RELACIONADO2 R2
  ON(R2.ID = EP.ID)
    
12.07.2017 / 16:09