简体   繁体   中英

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. 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. (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).

You don't need a sub select provided the column column_from_sometable belong to same table sometable . You can dimply include in WHERE condition like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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