如何在mysql的between子句中包含null datetime

时间:2017-04-06 10:45:09

标签: mysql sql

我尝试在devicetime =' 2017-04-06 10:06:57'和devicetime =' 2017-04-06 10:07:05'但结果并不包括具有空值的devicetime。

select * from positions where devicetime between '2017-04-06 10:06:57' and '2017-04-06 10:07:05' and deviceId=2

我在上面尝试了这个查询,但它没有用。

enter image description here

7 个答案:

答案 0 :(得分:0)

首先,您应该谨慎使用betweendatetime值。通常,您希望严格小于上限值。

无论如何,你只是在寻找基本逻辑:

select p.*
from positions p
where (device time is null or
       devicetime between '2017-04-06 10:06:57' and '2017-04-06 10:07:05'
      ) and
      deviceId = 2;

答案 1 :(得分:0)

您可以执行以下操作

select * from positions where (devicetime between '2017-04-06 10:06:57' and '2017-04-06 10:07:05' and deviceId=2) or devicetime is null

答案 2 :(得分:0)

所以只需添加一个空检查。

for an_array in mylist:

答案 3 :(得分:0)

我希望你想要这样的东西,应该在null device device中添加回来(但仍然是deviceId = 2)

select * from positions 
where ((devicetime between '2017-04-06 10:06:57' and '2017-04-06 10:07:05') or  devicetime IS NULL)
and deviceId=2

答案 4 :(得分:0)

假设在显示的数据之外还有其他行为NULL,devicetime基于select * from positions where id between ( select MAX(id) from positions where devicetime <= '2017-04-06 10:06:57' ) and ( select MIN(id) from positions where devicetime >= '2017-04-06 10:07:05' ) and deviceId=2 是顺序的:

  ( select id from positions
    where devicetime = '2017-04-06 10:06:57'
  )

如果您知道确切的时间,也可以使用

servertime

或者尝试使用devicetime代替if [ $0 == "test" ] || $0 == "all" ],这似乎是非空的。

答案 5 :(得分:0)

您可以在'2017-04-06 10:06:57'和'2017-04-06 10:07:05'或者devicetime = NULL和deviceId = 2 之间添加设备时间

答案 6 :(得分:0)

您也可以执行以下操作

1.第一种方式

while (1) {
    alert("hello, world!");
}

2.第二种方式

SELECT * FROM positions WHERE (devicetime BETWEEN '2017-04-06 10:06:57' AND '2017-04-06 10:07:05' AND deviceId=2) OR (devicetime IS NULL AND deviceId=2)