mysql统计查询

版权声明:本文为博主原创文章,转载请注明文章来源,尊重知识,分享别人标识作者是一种美德 https://blog.csdn.net/u010757785/article/details/78958283

好久没写博客了,不知道写撒,就写写统计吧

很多时候我们都会用到统计查询,在现在一个后台没有点什么折线图,什么图标展示都会显得这个后台一点不高大上,

列举一下表

drop table if exists oa_users;

/*==============================================================*/
/* Table: oa_users                                              */
/*==============================================================*/
create table oa_users
(
   u_id                 int not null auto_increment,
   u_type               char(2) default '1' comment '1:普通用户 2:会员用户3:超级vip用户 ',
   c_id                 int,
   u_name               varchar(32),
   u_addresscode        char(12),
   u_status             char(4),
   u_disable            char(2),
   u_password           varchar(64),
   u_uniond             varchar(64) binary,
   u_db                 bigint,
   primary key (u_id)
);
drop table if exists oa_chinnel;

/*==============================================================*/
/* Table: oa_chinnel                                            */
/*==============================================================*/
create table oa_chinnel
(
   c_id                 int not null auto_increment,
   c_name               varchar(32),
   c_remark             varchar(256),
   primary key (c_id)
);

下面来1,巧用group by

就那上面的表来说吧,例如用户类型有三种。

查询三种不同类型的用户

SELECT case when u_type=1 then 1 WHEN u_type = 2 THEN 2 else 3 end as names,COUNT(1) as countnumber FROM `oa_users` GROUP BY case when u_type=1 then 1 WHEN u_type = 2 THEN 2 else 3 end
这样就会获取数据库里面所有u_type三种类型的数据

上面的情况是知道类型的情况下面可以这么做,如果不知道类型呢

例如获取不同渠道用户的统计数量

select count(1) as countnumber from oa_users group by c_id
很简单,还有例如查询一年12个月的注册量,对上面oa_users表增加一个字段createtime   就可以了也可以用case when 

还可以用临时表,其实很多时候查询用临时表,时间会节约不少,还有一对多用inner join  

select t.myYear as 年份,t.monthNo as 月份,count(1) as 数量统计  from(select month(createtime) as monthNo, year(createtime) as myYear, u_id from oa_users) as t  where t.myYear='2016'  group by t.monthNo  
还有做出微博一样的热点中国地图,结合百度或者高德poi来做,文章里面有一个抢购的项目里面有百度poi地址,可以借鉴一下,为了公德,请使用自己的key,最近key请求饱满了,获取到用户区域code(adress_code)去地址表里面获取省市区,因为这是我的博客用户表,做了分表设计,所以这里看不出来,获取到的poi地址取第一个就是一个点了

猜你喜欢

转载自blog.csdn.net/u010757785/article/details/78958283