name   time1       time2      time3
a   2007-1-2   2007-1-3    null
b   2007-2-2   2007-2-3    null
b   2007-3-2   null        2007-1-3
b   2007-4-2   null        null
c   2007-5-2   2007-1-4    null
c   2007-6-2   2007-1-5    null
a   2007-7-2   2007-1-6    null求一检索,在一定时间段内,显示
name   count(time1) count(time2) count(time3)
a          1           1           0
b          3           1           2
c          1           1           0如果在时段内没有数据,全部显示为0谢谢师兄们赐教

解决方案 »

  1.   

    随便猜一下你想的的结果。select name
    sum(if(time1 between '2009-10-01' and '2019-10-01',1,0)) as `count(time1)`,
    sum(if(time2 between '2009-10-01' and '2019-10-01',1,0)) as `count(time2)`,
    sum(if(time3 between '2009-10-01' and '2019-10-01',1,0)) as `count(time3)`
    from xxxx
    where time1 between '2009-10-01' and '2019-10-01'
    or time2 between '2009-10-01' and '2019-10-01'
    or time3 between '2009-10-01' and '2019-10-01'
    group by name
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  3.   

    select name
        sum(if(time1 between '2009-10-01' and '2019-10-01',1,0)) as `count(time1)`,
        sum(if(time2 between '2009-10-01' and '2019-10-01',1,0)) as `count(time2)`,
        sum(if(time3 between '2009-10-01' and '2019-10-01',1,0)) as `count(time3)`
    from xxxx
    where time1 between '2009-10-01' and '2019-10-01'
    or time2 between '2009-10-01' and '2019-10-01'
    or time3 between '2009-10-01' and '2019-10-01'
    group by name
    //where time1 between '2009-10-01' and '2019-10-01'
    //or time2 between '2009-10-01' and '2019-10-01'
    //or time3 between '2009-10-01' and '2019-10-01'
    这一块不需要