mysql怎样实现或语句_mysql的sql语句怎么写?

现有表如下:idWATCH_TIMEONLINE_COUNT12012-3-716:40:42122012-3-717:48:55232012-3-718:48:071编写如下sql代码:SELECT(casewhenSUBSTR(WATCH_TIME,12,8)>='16:00:00'andSUBSTR(WAT...

现有表如下:

id WATCH_TIME ONLINE_COUNT

1 2012-3-7 16:40:42 1

2 2012-3-7 17:48:55 2

3 2012-3-7 18:48:07 1

编写如下sql代码:

SELECT (case when SUBSTR(WATCH_TIME,12,8)>='16:00:00' and SUBSTR(WATCH_TIME,12,8)<='16:59:59' then ONLINE_COUNT else 0 end) as h16,

(case when SUBSTR(WATCH_TIME,12,8)>='17:00:00' and SUBSTR(WATCH_TIME,12,8)<='17:59:59' then ONLINE_COUNT else 0 end)as h17,

(case when SUBSTR(WATCH_TIME,12,8)>='18:00:00' and SUBSTR(WATCH_TIME,12,8)<='18:59:59' then ONLINE_COUNT else 0 end)as h18,

(case when SUBSTR(WATCH_TIME,12,8)>='19:00:00' and SUBSTR(WATCH_TIME,12,8)<='19:59:59' then ONLINE_COUNT else 0 end)as h19

FROM `t_log_online_count` where date(WATCH_TIME)=date('2012-03-07');

获取结果如下:

h16 h17 h18 h19

1 0 0 0

0 2 0 0

0 0 1 0

期望获取的结果如下:

h16 h17 h18 h19

1 2 1 0

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值