explode () directly in MySQL [duplicate]

1

I have a column named relacionados in the produtos table and there are some ID's of some items in the produtos table. I needed to make a explode () or something of the type in the produtos table and select all products with the ID's that are in this relacionados table. Was there any possibility? The ID's are separated by , (commas).

Tabela = produtos
Coluna = relacionados

But I do not have to return how many ID's it has. What I need is to return as if it were multiple SELECTS with the ID's that are in the relacionados column. For example, in column relacionados I have: 2,3,5,10 so I needed to do something like this:

SELECT * FROM produtos WHERE id = 2
SELECT * FROM produtos WHERE id = 3
SELECT * FROM produtos WHERE id = 5
SELECT * FROM produtos WHERE id = 10
    
asked by anonymous 01.12.2015 / 17:21

1 answer

3
  

I'm waiting for the community to decide to see if it's a duplicate of the @rray recommended.

If values are separated by commas and you want to find one of them:

SELECT * WHERE FIND_IN_SET( 'b','a,b,c,d');

In your case it can be used like this:

SELECT * WHERE FIND_IN_SET( 'b', valores );

If they are separated by another character, you have a gambiarra a technical repair:

SELECT * WHERE FIND_IN_SET( 'b', REPLACE( coluna, '|', ',' ) );

If the need is another, edit the question with the relevant details, which update the answer.

    
01.12.2015 / 17:26