有一个客户日志表。包括客户cid,信息等级clevel(1,2,两个等级),信息状态cstatus (0,1两种状态)现在要求获取客户cid='111'统计信息,统计条件分别是的clevel=1,cstatus等于0和cstatus等于1的客户日志总数结果大概如下:客户cid 等级为1 状态为0 状态为1 111 12000 1000 500还有此表数据量比较大,谢谢拉
调试欢乐多
sum(case when clevel=1 then 1 esle 0 end) as [等级为1],
sum(case when cstatus=0 then 1 else 0 end) as [状态为0],
sum(case when cstatus=1 then 1 esle 0 end) as [状态为1]
from 客户日志表
group by cid
sum(if(clevel=1,1,0)) ,
sum(if(cstatus=0,1,0)),
sum(if(cstatus=1,1,0)) from tt where cid='111' group by cid
虽然这样写效率很低,但是有些方面想用一条sql语句出来,还就得按照这个模子套。