Mysql – Join query with IN condition and Group_concat

join;MySQL

I have an issue to get concatenated text from below two tables(product_to_category & category_description)

enter image description here

product_to_category

I need mysql query to get output as "Electronics,Electronics Component Connectors,Electronics Accessories" from category_description table's name column.That query will compare category_id in these tables with where condition as product_id = 276.

I tried this query,but it returns empty result –

 SELECT GROUP_CONCAT(description SEPARATOR ',') 
  FROM `ocrk_category_description` 
 WHERE `category_id` IN (SELECT GROUP_CONCAT(category_id SEPARATOR ',') as `category_id` 
                           FROM `ocrk_product_to_category` 
                          WHERE `product_id` = 276 
                        )

Any ideas? i can provide you more information if need.

Best Answer

I'm not 100% sure if I understand the question correct, but i'll try some solution!:

    SELECT a.product_id, group_concat(distinct b.name)
    FROM product_to_category a
    JOIN category_description b
    ON a.category_id = b.category_id
    GROUP BY a.product_id;

I hope this is what you were looking for...

Cheers, Olaf