记一次MySQL中datetime类型比较大小的坑

3,937 阅读1分钟

原本的SQL语句为:

select t3.id as groupId, t3.name as groupName, count(*) as total,
       sum(case when t1.state = '2' then 1 else 0) as pending,
       sum(case when t1.state = '2' then 1 else 0) as processing,
       sum(case when t1.state in ('4','5','0') then 1 else 0) as solved
       from question t1,customer t2,cmbgroup t3
       where t1.source_id = t2.id
       where t2.group_id = t3.id
       and 1575354492381(传入的时间戳) < t1.sta_dtm
       and t1.sta_dtm > 1576218492381(传入的时间戳)
       group by groupId

SQL是为了查询并统计在一段时间范围内一个部门下面所有分组的工作任务
但是一直都不返回数据,我把传入的时间戳换成mysql的内置函数now()可以查询到数据,而且只用第一个时间戳字段筛选,没有任何问题,但若是加上下面的筛选条件就查不到数据(下面的时间戳是now())

最后我将SQL语句改为如下:

select t3.id as groupId, t3.name as groupName, count(*) as total,
       sum(case when t1.state = '2' then 1 else 0) as pending,
       sum(case when t1.state = '2' then 1 else 0) as processing,
       sum(case when t1.state in ('4','5','0') then 1 else 0) as solved
       from question t1,customer t2,cmbgroup t3
       where t1.source_id = t2.id
       where t2.group_id = t3.id
       and 1575354492381(传入的时间戳) < t1.sta_dtm
       and unix_timestamp(t1.sta_dtm) > 1576218492381(传入的时间戳)
       group by groupId

疑问:为什么上面那行可以使用long类型的时间戳字段进行判断,而下面却需要加上unix_timestamp()?