简体   繁体   English

MYSQL查询时间戳比较

[英]MYSQL Query timestamp comparison

Given a table with a column that contains a timestamp, I would like to count the number of instances where the TIMEDIFF(now(), timeStamp) is greater than some value. 给定具有包含时间戳的列的表,我想计算TIMEDIFF(now(), timeStamp)大于某个值的实例数。 The issue I ran into is implementing this all into a statement. 我遇到的问题是将所有这些实现为声明。 My efforts: 我的努力:

SELECT COUNT*()
FROM sometable
WHERE (SELECT TIMEDIFF(now(),column_from_sometable) > 10

I'm just not quite sure how to use the TIMEDIFF in the where statement to use it for a comparison. 我只是不太确定如何在where语句中使用TIMEDIFF进行比较。 (Apparently you must use SELECT to call the method, and then I'm just not sure how to access the column from sometable in that statement). (显然,您必须使用SELECT来调用该方法,然后我不确定如何从该语句中的sometable表访问该列)。

You don't need a sub select provided the column column_from_sometable belong to same table sometable . 如果column_from_sometable列属于同一表sometable不需要子选择。 You can dimply include in WHERE condition like 您可以在WHERE条件中包括

SELECT COUNT(*)
FROM sometable
WHERE TIMEDIFF(now(),column_from_sometable) > 10

You can as well simplify this by adding the condition in your aggregate function like 您也可以通过在汇总函数中添加条件来简化此操作,例如

SELECT SUM(CASE WHEN TIMEDIFF(now(),column_from_sometable) > 10 THEN 1 ELSE 0 END)
FROM sometable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM