GROUP_CONCAT 是否带有附加分组?
我已经开始编写一个脚本来生成有关特定文章被调用的频率和时间的统计数据,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不能 100% 确定这会起作用,但这是一个开始:
在子查询中执行第二个
GROUP_CONCAT
。使用不同的计数(*)会隐藏具有相同计数的项目,这不是我认为你想要的,所以我将其以注释的形式放入。
顺便说一句,这里不需要
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.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, becauseGROUP BY
already sorts on title.