explode() right on Mysql

Asked

Viewed 1,867 times

1

I have a column called relacionados on the table produtos and there are some ID’s of some table items produtos. I needed to make a explode() or something like that on the table produtos and selects all products with the ID’s that are in this table relacionados. Would there be any possibility ? The Ids are separated by , (commas).

Tabela = produtos
Coluna = relacionados

But I don’t need to return how many ID’s I have. What I need is to return as several SELECTS with the ID’s in the column relacionados. For example, in the column relacionados I have so: 2,3,5,10 so I had 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
  • 1

    It’s just to compare a value? that answer resolves?

  • 1

    and how do you decide which line of "related" will pick up? If it is a second query, it is exactly what has already been answered. Assuming the column is in $related "SELECT * FROM produtos WHERE FIND_IN_SET( id, '$relacionado');"

1 answer

3

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

If the values are separated by a comma 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 gambit a technical repair:

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

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

  • I wanted to make a SELECT type multiple SELECTS with the ID’s in the column relacionados which are separated by "," (commas). My idea is as if to make "SELECT * FROM products WHERE key code = 3", "SELECT * FROM products WHERE key code = 2", etc.. Where in the product related column with the key code is 1 for example and in the column relacionados has the key code 2,3

Browser other questions tagged

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