GROUP_CONCAT 是否带有附加分组?

发布于 2024-11-04 00:04:22 字数 3089 浏览 3 评论 0原文

我已经开始编写一个脚本来生成有关特定文章被调用的频率和时间的统计数据,

SELECT `title`, `page_id`, COUNT(*) AS `total`, 
GROUP_CONCAT(DISTINCT `date_created` 
             ORDER BY `date_created` SEPARATOR ',') dates 
FROM `statistics` 
WHERE `supplier_id` = '27' 
GROUP BY `title` 
ORDER BY `title`;

该文章会产生

[0] => Array
        (
            [0] => Array
                (
                    [total] => 3
                    [dates] => 2011-04-26,2011-04-27
                )

            [statistics] => Array
                (
                    [title] => Title 2
                    [page_id] => 2
                )  
        )

[1] => Array
        (
            [0] => Array
                (
                    [total] => 6
                    [dates] => 2011-04-26,2011-04-27,2011-04-28
                )

            [statistics] => Array
                (
                    [title] => Title 7
                    [page_id] => 7
                )
        )

好东西。但是我如何添加一个类似于日期的字段,它添加了按 date_created 分组的 ids 的细分,从而创建类似的内容:

[2] => Array
    (
        [0] => Array
            (
                [total] => 6
                [dates] => 2011-04-26,2011-04-27,2011-04-28
                [total_by_date] => 2,1,3
            )

        [statistics] => Array
            (
                [title] => Title 7
                [page_id] => 7
            )
)

我想沿着这些线添加一个额外的 GROUP BY:

GROUP_CONCAT(DISTINCT COUNT(*) AS `total_by_date` 
             GROUP BY `date_created` ORDER BY `date_created` SEPARATOR ',')

但它不起作用,如何解决这个问题?

倾倒:

CREATE TABLE `statistics` (
  `id` int(11) NOT NULL auto_increment,
  `pagetype` varchar(50) character set utf8 collate utf8_unicode_ci NOT NULL,
  `supplier_id` int(11) NOT NULL,
  `page_id` int(11) default NULL,
  `title` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `date_created` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=920 ;

-- 
-- Dumping data for table `statistics`
-- 

INSERT INTO `statistics` (`id`, `pagetype`, `supplier_id`, `page_id`, `title`, `date_created`) VALUES 
(1, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(2, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(3, 'newsarticle', 27, 2751, 'Title 1', '2011-04-27'),
(4, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(5, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(6, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(7, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(8, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(9, 'newsarticle', 27, 462009, 'Title 2', '2011-04-28'),
(10, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(11, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(12, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(13, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(14, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26');

I have started on a script to generate statistics of how often and when a particular article is called using

SELECT `title`, `page_id`, COUNT(*) AS `total`, 
GROUP_CONCAT(DISTINCT `date_created` 
             ORDER BY `date_created` SEPARATOR ',') dates 
FROM `statistics` 
WHERE `supplier_id` = '27' 
GROUP BY `title` 
ORDER BY `title`;

which produces

[0] => Array
        (
            [0] => Array
                (
                    [total] => 3
                    [dates] => 2011-04-26,2011-04-27
                )

            [statistics] => Array
                (
                    [title] => Title 2
                    [page_id] => 2
                )  
        )

[1] => Array
        (
            [0] => Array
                (
                    [total] => 6
                    [dates] => 2011-04-26,2011-04-27,2011-04-28
                )

            [statistics] => Array
                (
                    [title] => Title 7
                    [page_id] => 7
                )
        )

Good stuff. But how can I add a field similar to dates which adds a breakdown of ids grouped by date_created thus creating something like:

[2] => Array
    (
        [0] => Array
            (
                [total] => 6
                [dates] => 2011-04-26,2011-04-27,2011-04-28
                [total_by_date] => 2,1,3
            )

        [statistics] => Array
            (
                [title] => Title 7
                [page_id] => 7
            )
)

I would like to add an extra GROUP BY along these lines:

GROUP_CONCAT(DISTINCT COUNT(*) AS `total_by_date` 
             GROUP BY `date_created` ORDER BY `date_created` SEPARATOR ',')

but it's not working, how to I fix this?

DUMP:

CREATE TABLE `statistics` (
  `id` int(11) NOT NULL auto_increment,
  `pagetype` varchar(50) character set utf8 collate utf8_unicode_ci NOT NULL,
  `supplier_id` int(11) NOT NULL,
  `page_id` int(11) default NULL,
  `title` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `date_created` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=920 ;

-- 
-- Dumping data for table `statistics`
-- 

INSERT INTO `statistics` (`id`, `pagetype`, `supplier_id`, `page_id`, `title`, `date_created`) VALUES 
(1, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(2, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(3, 'newsarticle', 27, 2751, 'Title 1', '2011-04-27'),
(4, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(5, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(6, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(7, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(8, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(9, 'newsarticle', 27, 462009, 'Title 2', '2011-04-28'),
(10, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(11, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(12, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(13, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(14, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-11-11 00:04:22

不能 100% 确定这会起作用,但这是一个开始:

在子查询中执行第二个 GROUP_CONCAT

SELECT title
  , s1.page_id
  , s2.total_by_date
  , GROUP_CONCAT(DISTINCT s1.date_created
                 ORDER BY s1.date_created SEPARATOR ',') as dates  
FROM statistics s1
INNER JOIN  
  (SELECT page_id
     ,GROUP_CONCAT(/*DISTINCT*/ gc.cnt
     ORDER BY gc.date_created SEPARATOR ',') AS total_by_date
   FROM 
   (
      SELECT count(*) as cnt
        ,date_created
      FROM statistics
      WHERE supplier_id = '27'
      GROUP BY date_created
   ) gc
   GROUP BY gc.date_created
  ) s2 ON (s1.page_id = s2.page_id)      
WHERE supplier_id = '27'  
GROUP BY title  
ORDER BY title; 

使用不同的计数(*)会隐藏具有相同计数的项目,这不是我认为你想要的,所以我将其以注释的形式放入。
顺便说一句,这里不需要 ORDER BY title,因为 GROUP BY 已经按标题排序。

Not 100% sure this is gonna work, but it's a start:

Do the 2nd GROUP_CONCAT in a sub-query.

SELECT title
  , s1.page_id
  , s2.total_by_date
  , GROUP_CONCAT(DISTINCT s1.date_created
                 ORDER BY s1.date_created SEPARATOR ',') as dates  
FROM statistics s1
INNER JOIN  
  (SELECT page_id
     ,GROUP_CONCAT(/*DISTINCT*/ gc.cnt
     ORDER BY gc.date_created SEPARATOR ',') AS total_by_date
   FROM 
   (
      SELECT count(*) as cnt
        ,date_created
      FROM statistics
      WHERE supplier_id = '27'
      GROUP BY date_created
   ) gc
   GROUP BY gc.date_created
  ) s2 ON (s1.page_id = s2.page_id)      
WHERE supplier_id = '27'  
GROUP BY title  
ORDER BY title; 

Using distinct count(*) would hide items with the same count, which is not I think you want, so I've put it in in commented form.
BTW the ORDER BY title is not needed here, because GROUP BY already sorts on title.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文