Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL组需要帮助吗_Mysql_Sql_Wordpress - Fatal编程技术网

MySQL组需要帮助吗

MySQL组需要帮助吗,mysql,sql,wordpress,Mysql,Sql,Wordpress,我尝试为我的WordPress安装创建一个随机的横幅插件,并且我喜欢从我的横幅中提取一些统计信息 为了执行该任务,我创建了一个具有以下签名的表: CREATE TABLE `bb_banner_statistics` ( `id` int(11) NOT NULL AUTO_INCREMENT, `post_id` bigint(20) DEFAULT NULL, `event` varchar(15) DEFAULT NULL, `value` tinyint(4) DEFAU

我尝试为我的WordPress安装创建一个随机的横幅插件,并且我喜欢从我的横幅中提取一些统计信息

为了执行该任务,我创建了一个具有以下签名的表:

CREATE TABLE `bb_banner_statistics` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) DEFAULT NULL,
  `event` varchar(15) DEFAULT NULL,
  `value` tinyint(4) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1
其中,
post\u id
是横幅id,
event
是我喜欢跟踪的事件(即查看、悬停、单击等),
value
只需取值“1”(我这样做是为了简化特定事件类型的统计),而
created
是事件的日期时间

因此,在示例中,该表可以包含如下值:

|---------------------------------------------------|
| ID | POST_ID | EVENT | VALUE | CREATED            |
|---------------------------------------------------|
| 1  | 62      | view  | 1     | 2014-4-12 11:45:12 |
| 2  | 63      | view  | 1     | 2014-4-12 11:45:12 |
| 3  | 64      | view  | 1     | 2014-4-12 11:45:12 |
| 4  | 62      | hover | 1     | 2014-4-12 11:46:18 |
| 5  | 63      | hover | 1     | 2014-4-12 11:46:22 |
| 6  | 63      | click | 1     | 2014-4-12 11:46:23 |
| 7  | 62      | hover | 1     | 2014-4-12 11:46:23 |
| 8  | 62      | view  | 1     | 2014-4-13 09:20:17 |
|---------------------------------------------------|

因此,问题是,如何对数据进行分组,以获得每天每个横幅的总视图、总点击量和总悬停事件数?

我想您需要条件聚合:

select date(created) as thedate, post_id,
       sum(event = 'click') as numclicks,
       sum(event = 'view') as numviews,
       sum(event = 'hover') as numhovers
from bb_banner_statistics
group by date(created), post_id;