select 分钟(现在()-ins_time)返回空值

select minute( now()-ins_time ) returning null

我有一个简单的报告,一些用户可以 运行 检查我们是否从外部源接收到数据。它计算自最后一行更新以来的分钟数,其中 INS_TIME 是接口插入数据的时间。

代码如下。

select minute( now()-ins_time )  as LastUpdateMinutes 
from p_electro 
order by ins_time desc limit 1

这很好用,并在 Crystal 报告中使用。然而,我正在尝试为不同的 table 制作另一个,但有点额外的复杂性。我复制了上面的代码并添加如下。

select minute( now()-ins_time )  as LastUpdateMinutes 
from p_treats
where location like '%e5%' and creator is null
order by ins_time desc limit 1

这个标准应该基本上表明该行是由界面插入的,然后显示自上次输入以来的分钟数。然而,当我 运行 它时,它 运行 成功但我得到一个 NULL 返回(如下所示)。

LastUpdateMinutes
-----------------
|   NULL        |

鉴于自 2017 年 5 月 13 日上午 11 点以来我们没有收到任何数据,我原以为会看到数千分钟。

这对第一个 table 来说效果很好,并且已经使用了多年。

为了添加一些背景信息,我们的数据管理员 运行 每天两次报告此报告,并报告时间(以分钟为单位)是否超过 20 分钟。对于第二个 table,我们已经 5 天没有收到数据(多亏了这次网络攻击后采取的措施),直到我脑子里的灯泡亮起才意识到这可能受到了影响.

我想为此创建另一个报告table,我们可以将其添加到办事员的日常检查中,并尽快了解缺失的数据

有人对为什么第二个示例不起作用但第一个示例起作用有任何建议吗?以及如何修复它?

我尝试了多种语法:

SELECT 
datediff(now(), ins_time) AS DiffDate
FROM
p_treats
WHERE
location LIKE 'e5%' and 
creator is null
ORDER BY ins_time DESC
LIMIT 1

这实际上可以缩短天数并且:

SELECT 
timediff(now(), ins_time) AS DiffDate
FROM
p_hdtreatment
WHERE
hpwhere LIKE 'e5%' and 
creator is null
ORDER BY ins_time DESC
LIMIT 1 

returns 小时和这实际上可能是我们使用的,但我想让它更准确一些。

提前致谢

试试这个。

SELECT TIMESTAMPDIFF(MINUTE, ins_time, now()) AS LastUpdateMinutes
from p_treats
where location like '%e5%' 
and creator is null
order by ins_time desc limit 1

结帐 -> MySQL TimeStampDiff

旁注:

minute() 在第一个 "working" 示例中按原样使用,我相信会给您错误的答案.. minute() 仅提取 date/time 字符串的分钟部分。例如。 SELECT MINUTE('2008-02-03 10:05:03') 产生 5

假设 "ins_time" 的数据类型为 "TimeStamp" 或 "DateTime",如果 MINUTE(now() - ins_time) 的结果大于 1 小时,则您的答案是错误的。我需要对此进行更多测试才能确定。