有一表 tms
字段 
id auto_increment
TeamID int(11)    UNSIGNED 
Set1 tinyint(4)    UNSIGNED 
OT1 tinyint(4)    UNSIGNED 
Select count(*) as mCount,if(OT1!=0,sum(1),false) as cOT1 From `tms` group by TeamIDcount(*) as mCount =>所有符合条件的记录数量if(OT1!=0,sum(1),false) as cOT1 => OT1 不等于0的记录的数量.为何得出来的数量(if(OT1!=0,sum(1),false) as cOT1) 不正确的,怎样改?

解决方案 »

  1.   

    改成这样:
    Select count(*) as mCount,sum(if(OT1!=0,1,0)) as cOT1 From `tms` group by TeamID 
      

  2.   

    id auto_increment 
    TeamID int(11)    UNSIGNED 
    Set1 tinyint(4)    UNSIGNED 
    OT1 tinyint(4)    UNSIGNED 
    错误:
    Select count(*) as mCount,if(OT1!=0,sum(1),false) as cOT1 From `tms` group by TeamID 
    首先你的if是条件句,只能走一次。
    要循环,但是可以子查询
    正确:
    Select count(*) as mCount,
    (select count(×)from `tms`  where OT1!=0) as cOT1  from 'tms' group by TeamID