如图 有数据如下:
+-----+----------------------------------+--------+------------+------+
| sid | jession_id                       | vip_id | subtime    | hits |
+-----+----------------------------------+--------+------------+------+
|   1 | B6FACFF5BEFB0B530F62AD1D159C0BDC |        | 2009-04-13 |    3 |
|   2 | B6FACFF5BEFB0B530F62AD1D159C0BDC | 1848   | 2009-04-13 |    5 |
|   3 | B6FACFF5BEFB0B530F62AD1D159C0B22 | 7906   | 2009-04-12 |    5 |
|   4 | B6FACFF5BEFB0B530F62AD1D159C0BDC | 7926   | 2009-04-12 |    1 |
+-----+----------------------------------+--------+------------+------+我想得到的结果是:
当查询条件subtime为空时hits       count(*)
5            2
1            1
3            1当查询条件为subtime=’2009-04-13‘时hits       count(*)
5            1
3            1

解决方案 »

  1.   

    select hits,count(*) from tt 
    where if(@subtime is null ,length(hits)>=1,subtime=@subtime) group by hits
      

  2.   

    or
    select hits,count(*) from tt
    where if(@subtime is null ,1=1,subtime=@subtime) group by hits
      

  3.   

    还是不明白怎么按条件查询呢?@subtime这个是什么意思...........可否耐心解释一下啊?
      

  4.   

    @subtime:变量名,进入查询条件,比如为空 OR 2009-04-13