A表
id,cpid,regTime,actTimecpid:用户组id
regTime:注册时间
actTime:最后活跃时间我想查看2013-05-01到2013-05-30号中所有用户组有多少个注册,有多少个活跃查询结果应如下
cpid    regCount   actCount
1          200         111
2          245        1
3          244        323需要考虑的是,如果某条数据注册时间不在范围内,而活跃时间在,或者活跃在范围内,注册不在还有可能日期不一定1-30每天都有数据,可能中间有断开,那么需要显示0和0.
请指教

解决方案 »

  1.   

    select cpid, sum(if(regTime >='2013-05-01 00:00:00' and regTime<'2013-05-31 00:00:00'),1,0),sum(if(actTime >='2013-05-01 00:00:00' and actTime <'2013-05-31 00:00:00'),1,0) from A group by cpid
      

  2.   

    select cpid, sum(if(regTime >='2013-05-01 00:00:00' and regTime<'2013-05-31 00:00:00',1,0)),sum(if(actTime >='2013-05-01 00:00:00' and actTime <'2013-05-31 00:00:00',1,0)) from A group by cpid
    上面的错了
      

  3.   

    select cpid,
    sum(if(regTime between '2013-05-01' and '2013-05-30',1,0)) as regCount,
    sum(if(actTime between '2013-05-01' and '2013-05-30',1,0)) as actCount
    from A表
    group by cpid
      

  4.   


    我忘了一个重要的字段 时间字段 显示应该是这样
    查询结果应如下
     cpid    regCount   actCount   time
     1          200        111    2013-05-01
     2          245        0      2013-05-02
     3          0          32     2013-05-03
      

  5.   


    我忘了一个重要的字段 时间字段 显示应该是这样
     查询结果应如下
      cpid    regCount   actCount   time
      1          200        111    2013-05-01
      2          245        0      2013-05-02
      3          0          32     2013-05-03 
      

  6.   


    我忘了一个重要的字段 时间字段 显示应该是这样
     查询结果应如下
      cpid    regCount   actCount   time
      1          200        111    2013-05-01
      2          245        0      2013-05-02
      3          0          32     2013-05-03 4楼已经说的很清楚了啊,如果你加的是时间字段,在select后面在加一个查询结果就可以了。
    推荐你看看Mysql子查询,多表查询的一些文档。