Hello,
I have products table like below
Table products {
-product_id- -categories-
1 2,4,5,6
2 1,4,3
4 3,5
}
Table categories {
-category_id- -en_label(catname)-
1 cat1
2 cat2
3 cat3
4 cat4
5 cat5
6 cat6
}
i want to display categorynames using sigle query
productid en_label
1 cat2,cat4,cat5,cat6
2 cat1,cat4,cat3
i wrote for this
SELECT m.product_id as product_id, m.product_en as product_en
, CONCAT(c1.en_label, if(c2.en_label IS NULL,'',', '), ifnull(c2.en_label,'')) as categories
FROM tbl_products m
LEFT JOIN tbl_categories c2 ON
(replace(substring(substring_index(m.cat_id, ',', 2),
length(substring_index(m.cat_id, ',', 2 - 1)) + 1), ',', '') = c2.category_id)
INNER JOIN tbl_categories c1 ON
(replace(substring(substring_index(m.cat_id, ',', 1),
length(substring_index(m.cat_id, ',', 1- 1)) + 1), ',', '') = c1.category_id)
it s displaying only for two lists like(1,4). but there are more than cat ids are i list it is not showing. can anybody tell me how to write mysql query for this using joins
Thanks,
Murali