MySQL group_concat and count

web-nomad

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:

PHPMyAdmin 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:

enter image description here

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.

M Khalid Junaid

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 ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL : GROUP_CONCAT the column and its count

From Dev

MYSQL get count of search results with group_concat

From Dev

MySQL Group_Concat Not In

From Dev

Count group_concat results

From Dev

Mysql group_concat of repeated keys and count of repetition of multiple columns in 1 query ( Query Optimization )

From Dev

Mysql group_concat for count in sub query using parent filed is not allowed

From Dev

How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?

From Dev

If condition with group_concat in mysql

From Dev

Mysql with GROUP_CONCAT in subselect

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

MySQL group_concat with join

From Dev

mySQL GROUP_CONCAT - Query

From Dev

MySQL: Nested GROUP_CONCAT

From Dev

Inversing group_concat in Mysql

From Dev

MySQL group_concat problems

From Dev

MySQL group_concat and nest with another group_concat

From Dev

MySQL group_concat and nest with another group_concat

From Dev

group_concat doesn't work with count

From Dev

MySQL GROUP_CONCAT excluding group value

From Dev

Mysql - Possible solutions for "group by group_concat()"?

From Dev

Mysql with Group by + IF + Left Join + Group_Concat

From Dev

GROUP BY inside GROUP_CONCAT in MySQL @ MariaDB

From Dev

mysql inner join group_concat mysql

From Dev

MySQL Group_Concat two tables together

From Dev

mysql multiple group_concat order preservation

From Dev

MySQL Use GROUP_CONCAT with Multiple JOINS

From Dev

MySQL GROUP_CONCAT with COLUMN SPLIT

From Dev

MySQL group_concat SEPERATOR char or ascii

From Dev

Optimize MySQL query for group_concat function