我已经开始使用脚本来生成使用
调用特定文章的频率和时间的统计信息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分组的ID细分,从而创建如下内容:
[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 ',')
但它不起作用,我该如何解决这个问题?
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');
答案 0 :(得分:1)
不是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
已对标题进行排序。