3

Is it that possible to select in Group_concat with separator comma and search where concat by comma Here My sample of mysql :

products_attributes_id  |products_id|   options_id|     options_values_id 
39                  |   31      |       3     |         3   
35                  |   30      |       2     |         2   
38                  |   30      |       1     |         1   
40                  |   31      |       2     |         2   
41                  |   30      |       1     |         4   
42                  |   30      |       1     |         5   
43                  |   31      |       1     |         4 

I want to GROUP_CONCAT options_values_id

SELECT * , GROUP_CONCAT(options_values_id SEPARATOR ',') FROM products_attributes GROUP by products_id

products_attributes_id  |   products_id |   options_id  |   options_values_id   |   options_values_id
35                  |   30          |   2           |   2                   |   2,1,4,5
39                  |   31          |   3           |   3                   |   3,2,4

Now my problem is How I will find the product_id by searching ( option_values_id with comma) if my value is 1,2 or 2,4 then I will get only product_id=30. But if I got value 1,3 I will get nothing. Because product_id 30 don't have option_values_id 3 and product_id 31 don't have option_values_id 1.

I try this code but I can't get the product_id

    SELECT * , GROUP_CONCAT( options_values_id
SEPARATOR ',' )
FROM `products_attributes` WHERE CONCAT(',',options_values_id,',') LIKE '%,1,2,%'
GROUP BY products_id
1
  • Maybe there is a way to do this without using GROUP_CONCAT? Can you give us some more background info? Commented Jun 6, 2013 at 9:50

1 Answer 1

9

Try this:

SELECT 
    products_id , 
    GROUP_CONCAT(options_values_id SEPARATOR ',') as ops 
FROM 
    products_attributes 
GROUP by 
    products_id
HAVING
    FIND_IN_SET('1',ops)
    OR FIND_IN_SET('3',ops)
Sign up to request clarification or add additional context in comments.

1 Comment

"Try this" answers are low value on Stack Overflow because they do very little to educate/empower thousands of future researchers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.