数据库统计查询

来源:互联网 发布:李氏筋骨散淘宝 编辑:程序博客网 时间:2024/06/06 00:31
查询agentuid相同的数据 并安装agentuid的数目进行排行

select count(*) as total,agentuid from `ss_member` where agentuid>0 group by agentuid order by total desc limit 100

根据id号 查出其他字段的总额

第一种
select uid,sum(money) as moneytotal,sum(votes) as votestotal  from cmf_users_cashrecord group by uid
第二种
select uid,sum(money) as moneytotal,sum(votes) as votestotal  from cmf_users_cashrecord where uid=100
第三种(说明 将familyid 和uid相同的用户 以天为单位 将profit进行统计)
select uid,sum(profit) as profitzong from cmf_family_profit where familyid=100 group by uid,time desc


统计不同值下的数量

select sum(case when one_uid='10251' then 1 else 0 end) as one,sum(case when two_uid='10252' then 1 else 0 end) as two from xzb_users_agent

查询两个字段在不同条件下差的和

select
sum(case when bet_type=1 then coin else 0 end  - case when bet_type=1 then IFNULL(kick_conn,0) else 0 end) as one,
sum(case when bet_type=2 then coin else 0 end  - case when bet_type=2 then IFNULL(kick_conn,0) else 0 end) as two,
sum(case when bet_type=3 then coin else 0 end  - case when bet_type=3 then IFNULL(kick_conn,0) else 0 end) as three,
sum(case when bet_type=4 then coin else 0 end  - case when bet_type=4 then IFNULL(kick_conn,0) else 0 end) as four,
sum(case when bet_type=5 then coin else 0 end  - case when bet_type=5 then IFNULL(kick_conn,0) else 0 end) as five
from  cmf_users_game_bet
where  square_type=0


原创粉丝点击