我试图在单个查询中获取每个类别的方面,如下所示:
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;
此查询给出了以下结果:
正如您所看到的,类别的所有方面(在本例中为移动设备)都在一个单独的行中。我想要的是将所有类别的所有方面都放在一行中。所以,我尝试这个查询:
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;
这给出了以下结果:
如您所见,count
已针对手机类别增加。 Mobiles只有271项,但第二个查询几乎将此数字乘以no。该类别的方面。
我不确定为什么会这样。任何想法都会受到欢迎。
提前致谢。
答案 0 :(得分:3)
表deals
中的重复ID可能会在您的计数功能中使用DISTINCT
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 ...