MySQL统计过去12个月的数据没有则为0
2015-12-18 09:25
483 查看
查询每月新增用户数
celsp_users是用户表
createTime 为新增用户时间
SELECT a.count,b.time2 from
(select count(c.createTime) as count,FROM_UNIXTIME( UNIX_TIMESTAMP(c.createTime),'%m' ) as mon
from celsp_users c where FROM_UNIXTIME( UNIX_TIMESTAMP(c.createTime),'%Y' ) = '2015' GROUP BY mon) a
RIGHT JOIN (SELECT '01' as time2 from DUAL UNION ALL
SELECT '02' as time2 from DUAL UNION ALL
SELECT '03' as time2 from DUAL UNION ALL
SELECT '04' as time2 from DUAL UNION ALL
SELECT '05' as time2 from DUAL UNION ALL
SELECT '06' as time2 from DUAL UNION ALL
SELECT '07' as time2 from DUAL UNION ALL
SELECT '08' as time2 from DUAL UNION ALL
SELECT '09' as time2 from DUAL UNION ALL
SELECT '10' as time2 from DUAL UNION ALL
SELECT '11' as time2 from DUAL UNION ALL
SELECT '12' as time2 from DUAL )b on a.mon = b.time2
结果集
celsp_users是用户表
createTime 为新增用户时间
SELECT a.count,b.time2 from
(select count(c.createTime) as count,FROM_UNIXTIME( UNIX_TIMESTAMP(c.createTime),'%m' ) as mon
from celsp_users c where FROM_UNIXTIME( UNIX_TIMESTAMP(c.createTime),'%Y' ) = '2015' GROUP BY mon) a
RIGHT JOIN (SELECT '01' as time2 from DUAL UNION ALL
SELECT '02' as time2 from DUAL UNION ALL
SELECT '03' as time2 from DUAL UNION ALL
SELECT '04' as time2 from DUAL UNION ALL
SELECT '05' as time2 from DUAL UNION ALL
SELECT '06' as time2 from DUAL UNION ALL
SELECT '07' as time2 from DUAL UNION ALL
SELECT '08' as time2 from DUAL UNION ALL
SELECT '09' as time2 from DUAL UNION ALL
SELECT '10' as time2 from DUAL UNION ALL
SELECT '11' as time2 from DUAL UNION ALL
SELECT '12' as time2 from DUAL )b on a.mon = b.time2
结果集
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 我是运营,我没有假期
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志
- MySQL 安全事宜