sql - MySQL group_concat and count -
i trying aspects of each category in single query follows:
select b.`id` parent_id, b.`name` parent_name, a.`id` child_id, a.`name` child_name, a.`pageid` t_id, count( c.`id` ) deals_in_cat, d.`aspect_values` aspects `category_parent` left join `navigation_filters_weightage` d on a.`id` = d.`cat_id`, `deals_parent_cat` b, `deals` c a.`parent_id` = b.`id` , c.`ebaydeals_category` = a.`id` group a.`id`, d.`frequency` order b.`order` asc, a.`order` asc, d.`frequency` desc;
this query gives me following result:
as can see, aspects of category (mobiles, in case) in separate row. want aspects of categories in single row. so, try query:
select b.`id` parent_id, b.`name` parent_name, a.`id` child_id, a.`name` child_name, a.`pageid` t_id, count( c.`id` ) deals_in_cat, group_concat( distinct d.`aspect_values` order d.`frequency` desc ) aspects `category_parent` left join `navigation_filters_weightage` d on a.`id` = d.`cat_id`, `deals_parent_cat` b, `deals` c a.`parent_id` = b.`id` , c.`ebaydeals_category` = a.`id` group a.`id` order b.`order` asc , a.`order` asc;
this gives below result:
as see, count
has increased mobiles category. there 271 items mobiles, second query multiplies number no. of aspects category.
i not sure why happening. ideas welcome.
thanks in advance.
there may repeated ids table deals
try using distinct
in count function
select b.id parent_id, b.name parent_name, a.id child_id, a.name child_name, a.pageid t_id, count(distinct c.id ) deals_in_cat ...
Comments
Post a Comment