简体   繁体   中英

get the count of field and group concat

table structure is as follows -- Table structure for table category

CREATE TABLE `category` (
  `cat_id` int(10) NOT NULL auto_increment,
  `heading` varchar(255) NOT NULL,
  PRIMARY KEY  (`cat_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `category` (`cat_id`, `heading`) VALUES
(1, 'Fashion'),
(2, 'Kids');

-- --------------------------------------------------------

-- Table structure for table `shop`

CREATE TABLE `shop` (
  `store_id` int(10) NOT NULL auto_increment,
  `shop_name` varchar(255) NOT NULL,
  `cat_id` int(10) NOT NULL,
  `subcat_id` int(10) NOT NULL,
  PRIMARY KEY  (`store_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `shop` (`store_id`, `shop_name`, `cat_id`, `subcat_id`) VALUES
(1, 'Test Store', 1, 1),
(2, 'Test Store 1', 1, 1),
(3, 'Another Store', 1, 3);

-- --------------------------------------------------------

-- Table structure for table `subcategory`

CREATE TABLE `subcategory` (
  `subcat_id` int(10) NOT NULL auto_increment,
  `cat_id` int(10) NOT NULL,
  `heading` varchar(255) NOT NULL,
  PRIMARY KEY  (`subcat_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

INSERT INTO `subcategory` (`subcat_id`, `cat_id`, `heading`) VALUES
(1, 1, 'Women'),
(2, 1, 'General'),
(3, 1, 'Men'),
(4, 2, 'Children');

if i use the below query i get the following output

SELECT
    `category`.`heading` AS `category`
    , `subcategory`.`heading` AS `subcategory`
    , COUNT(`shop`.`subcat_id`) AS cnt
FROM
    `test`.`shop`
    INNER JOIN `test`.`subcategory` 
        ON (`shop`.`subcat_id` = `subcategory`.`subcat_id`)
    INNER JOIN `test`.`category` 
        ON (`shop`.`cat_id` = `category`.`cat_id`)
GROUP BY `shop`.`subcat_id`
HAVING (COUNT(`shop`.`subcat_id`) !='');

category subcategory cnt
Fashion Women 2
Fashion Men 1

but i want to group concat the subcategory like below

category subcategory
Fashion Women,2|Men,1

Try this

SELECT t.category,
GROUP_CONCAT(CONCAT(t.subcategory,',',t.cnt) SEPARATOR '|') `concat`
FROM (
SELECT
    `category`.`heading` AS `category`
    , `subcategory`.`heading` AS `subcategory`
    , COUNT(`shop`.`subcat_id`) AS cnt
FROM
    `shop`
    INNER JOIN `subcategory` 
        ON (`shop`.`subcat_id` = `subcategory`.`subcat_id`)
    INNER JOIN `category` 
        ON (`shop`.`cat_id` = `category`.`cat_id`)
GROUP BY `shop`.`subcat_id`
) t
GROUP BY t.category

Note group concat has a default limit of 1024 character but it can be increased by following the manual

Fiddle Demo

Not a recommended output format, but easily done with a nested subquery:

SELECT category,
       group_concat(subcategory, ',', cnt separator '|') as vals
FROM (SELECT c.`heading` AS `category`, sc.`heading` AS `subcategory`,
             COUNT(`shop`.`subcat_id`) AS cnt
      FROM `test`.`shop` s INNER JOIN
           `test`.`subcategory`  sc
           ON s.`subcat_id` = sc.`subcat_id`) INNER JOIN
           `test`.`category` c
           ON s.`cat_id` = c.`cat_id`
      GROUP BY  c.`heading`, sc.`heading`
     ) sc
GROUP BY category;

Your having clause is unnecessary. It is just checking that there is at least one row for each group. But there is one, because you are using inner join .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM