在mysql查询中处理空时间

时间:2017-03-13 15:28:45

标签: mysql

我想要运行如下的查询:

SELECT ...
JOIN ...
WHERE ...
AND upload.updated + interval 1 minute <= NOW()

问题是upload.updated可以是NULL,如果是,则该语句不返回任何内容。我想要它,如果它不是null,请检查它是否为<= NOW()。这可能吗?

1 个答案:

答案 0 :(得分:2)

如果您希望查询返回更新为NOT NULL且超过一分钟的行,但排除更新为NULL的行:

WHERE ...
 AND upload.updated IS NOT NULL 
 AND upload.updated + interval 1 minute <= NOW()

如果您希望查询返回更新时间超过一分钟的行,并且还包括更新为NULL的行:

WHERE ...
 AND (upload.updated IS NULL 
  OR upload.updated + interval 1 minute <= NOW())