I am trying to get the aspects of each category in a single query as follows:
SELECT b.`id` AS parent_id, b.`name` AS parent_name,
a.`id` AS child_id, a.`name` AS child_name, a.`pageid` AS t_id,
COUNT( c.`id` ) AS deals_in_cat,
d.`aspect_values` AS aspects
FROM `category_parent` AS a
LEFT JOIN `navigation_filters_weightage` AS d ON a.`id` = d.`cat_id`,
`deals_parent_cat` AS b,
`deals` AS c
WHERE a.`parent_id` = b.`id`
AND c.`ebaydeals_category` = a.`id`
GROUP BY a.`id`, d.`frequency`
ORDER BY b.`order` ASC, a.`order` ASC, d.`frequency` DESC;
This query gives me the following result:
As you can see, all the aspects of a category (Mobiles, in this case) are in a separate row. What i want is to get all aspects of all categories in a single row. So, i try this query:
SELECT b.`id` AS parent_id, b.`name` AS parent_name,
a.`id` AS child_id, a.`name` AS child_name, a.`pageid` AS t_id,
COUNT( c.`id` ) AS deals_in_cat,
GROUP_CONCAT( DISTINCT d.`aspect_values` ORDER BY d.`frequency` DESC ) AS aspects
FROM `category_parent` AS a
LEFT JOIN `navigation_filters_weightage` AS d ON a.`id` = d.`cat_id`,
`deals_parent_cat` AS b,
`deals` AS c
WHERE a.`parent_id` = b.`id`
AND c.`ebaydeals_category` = a.`id`
GROUP BY a.`id`
ORDER BY b.`order` ASC , a.`order` ASC;
This gives the below result:
As you see, the count
has increased for mobiles category. There are only 271 items for Mobiles, but the second query almost multiplies this number by the no. of aspects for that category.
I am not sure why is this happening. Any ideas would be welcome.
Thanks in advance.
There may be repeated ids from table deals
try using DISTINCT
in your count function
SELECT b.id AS parent_id,
b.name AS parent_name,
a.id AS child_id,
a.name AS child_name,
a.pageid AS t_id,
count(DISTINCT c.id ) AS deals_in_cat ...