用时间条件查询

时间:2016-11-25 08:54:18

标签: mysql

我的查询

SELECT cookie_name, 
       SUBTIME( CURTIME( ), 
       min( time ) ) AS spent <='00:01:00', 
       date, sum( cookie_view ) AS views 
FROM cookie_table  
GROUP BY cookie_name, date 
    HAVING sum( cookie_view ) =1 

获得输出

                  cookie_name      spent               date   views   
                  5gm7aXUvkbhh   00:25:44.000000     25/11/16    1
                  ZR7929L32Bq    00:28:30.000000     25/11/16    1

但我想要这样的观看次数和花费&lt;该查询中1分钟,2分钟和> 3分钟。

                       date      views    <1   <2   >3
                       25/11/16    2       0    0    2

有可能吗?这里花的是&gt; 3所以它的节目记录数为2(00:03:00&lt; 00:25:44.000000,00:28:30.000000)

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

SELECT `cookie_name`, 
       SUBTIME( CURTIME( ), 
       min( time ) ) AS `spent` <='00:01:00', 
       date, sum( `cookie_view` ) AS `views` ,
       sum(case `spent` when < '00:01:00' then 1 else 0 end) as `one`,
       sum(case `spent` when < '00:02:00' then 1 else 0 end) as `two`,
       sum(case `spent` when > '00:03:00' then 1 else 0 end) as `three`
FROM `cookie_table` 
GROUP BY `cookie_name`, `date` 
    HAVING sum( `cookie_view` ) =1