mysql - 如何使用 Group By 选择 1 到 5 天的数据

标签 mysql sql mariadb mariasql

我在这里尝试过,但不行,只能显示总数。

我有一个下载表和一个程序表。

每次下载程序时,我都会记录日期和时间,我需要对下载的程序进行分组,然后将日期分成 5 列,这是一个示例。

PROGRAMA   |  HOJE  | ONTEM| 2 DIAS | 3 DIAS | 4 DIAS

Programa 1     11      110     55       66      12  
Programa 2     25      140     60       90      12
Programa 3     10      20      20       10      10  
TOTAL          46      270     135      166     32

下面是我的查询

select `k`.`app_id` AS `app_id`,`b`.`aplicativo` AS `aplicativo`,count(0) AS `HOJE`,

(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id`= `b`.`id`    and created_at  > (cast(now() as date) - interval 1 day) and (`a`.`created_at` < cast(now() as date)- interval 0 day) ) as ONTEM ,

(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
 and created_at  > (cast(now() as date) - interval 2 day) and (`a`.`created_at` < cast(now() as date)- interval 1 day) ) as 2_DIAS_ANTES ,

(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
 and created_at  > (cast(now() as date) - interval 3 day) and (`a`.`created_at` < cast(now() as date)- interval 2 day) ) as 3_DIAS_ANTES ,

(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
 and created_at  > (cast(now() as date) - interval 4 day) and (`a`.`created_at` < cast(now() as date)- interval 3 day) ) as 4_DIAS_ANTES ,

(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
 and created_at  > (cast(now() as date) - interval 5 day) and (`a`.`created_at` < cast(now() as date)- interval 4 day) ) as 5_DIAS_ANTES 


from (`registration` `k` join `aplicativos` `b`) where ((`k`.`app_id` = `b`.`id`) and (`k`.`created_at` > (cast(now() as date) - interval 0 day)))

group by `b`.`aplicativo`

表格结构

表格应用

CREATE TABLE IF NOT EXISTS `aplicativos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_usuario` int(11) NOT NULL,
  `aplicativo` varchar(200) NOT NULL,
  `link` varchar(400) NOT NULL,
  `quantidade_notificacoes` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

表注册

CREATE TABLE IF NOT EXISTS `registration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gcm_regid` varchar(300) NOT NULL,
  `app_id` int(11) NOT NULL,
  `email` varchar(200) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=73876 ;

最佳答案

这是 MySQL 的一种方法:

SELECT a.aplicativo as PROGRAMA,
      sum(Date(r.created_at) = CURDATE()) AS HOJE,
      sum(date(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY), 1, 0)) AS ONTEM,
       ...
FROM registration r INNER JOIN 
     aplicativos a
     on r.app_id = a.id
GROUP BY r.app_id ;

关于mysql - 如何使用 Group By 选择 1 到 5 天的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203180/

相关文章:

mysql - 当另一个表中没有行时如何从MySQL中选择一行

sql - 有没有更好的方法来构建我的表,进行 4 表连接,或者我是否必须执行多个查询?

mysql - 与 XAMPP 相比,MariaDB Docker 容器中的 INSERT SQL 查询非常慢

mysql - sql : cannot drop foreign key due to auto-generated constraint

mysql - cakephp中mysql表的creation_time字段困惑

php - query_cache_min_res_unit;它是什么以及它有什么作用?

mysql - 查询 order by(两列的乘积)

SQL Server : Select all but the first result in query

sql - 如何在 SQL 中使用变量引发错误

mysql - 无法在 mariadb 中创建 FUNCTION