Mysql - Select comparing fields from the same table

Asked

Viewed 1,413 times

0

I know it’s supposed to be simple, but I’m having a hard time getting through this. I have a table called "enterprise". In this table, I have 4 specific fields that store different ID’s from other records in the same table.

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

 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 select, showing for example, all records with id equal to the fields of the related tables1, related2, related3, related4.

How do I do that? I’m not getting it any way. Thanks in advance!

  • explain to me why you save data from different ID’s from other records in that same table? This is the only way to do what you need to do?

  • It’s kind of hard to understand what you want. I think your data modeling has not been done well. Do you want the table rows where the related fields1, related2, related3 and related4 are the same? Put in your example data that provides an expected return and which would be it.

  • whereas it is mere WHERE valor = relacionado1 AND valor = relacionado 2 or even WHERE relacionado1 = relacionado2 AND relacionado2 = relacionado3 AND ..., it would be important to [dit] putting the attempt and describing what went wrong to see where the problem is.

2 answers

1

Good afternoon you can make a select by putting nicknames in the table, for example.

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
Select  relacionado1  , relacionado2 from  empreendimento A
inner join empreendimento B
ON a.relacionado1 = b.relacionado2
WHERE a.relacionado=10

this way will bring the ids Query return

relacionado1   relacionado2    
10             10              

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.