Time Since Update MySQL Query
I have table full of timestamps similar to 2011-10-05 08:09:00.000
this is queried by the below query referencing reg_date, mid_date, pre_date. Instead of displaying the timestamp I need it to display 23 mins ago
instead of 2011-10-05 08:09:00.000
. I have tried some functions like timediff()
and format()
. I would like to complete this all in the query so that I do not have to query and the process the results.
Maybe I am not understanding the manaul very well but I am sure there is a way to complete this. I am really new to MySQL query statement. I see lots of potential and time saving though. I just need a little bit of help to get ths accomplished.
select reg_price as regPrice, _id as ID, lat, lng, reg_date as regDate,
mid_date as midDate, pre_da开发者_StackOverflowte as preDate,
format((acos(sin(radians(39.9891)) * sin(radians(lat)) + cos(radians(39.9891)) *
cos(radians(lat)) * cos(radians(-82.8116) - radians(lng))) * 6378),1)
as distance from stationDetails where (acos(sin(radians(39.9891))
* sin(radians(lat)) + cos(radians(39.9891)) * cos(radians(lat)) *
cos(radians(-82.8116) - radians(lng))) * 6378) <= 3 order by reg_price asc, reg_price asc
Using TIMEDIFF()
and TIME_FORMAT()
you should be able to display results like X minutes ago. It should be easy to modify this for other time values as needed, depending on how you want to deal with different timescales (hours, days, etc.):
select
TIME_FORMAT(
TIMEDIFF(now(), dt)
,'%i minute(s) ago'
)
from Data;
Demo: http://sqlize.com/r1mlXcM62V
This the function that was built for time span similar to SOF.
DELIMITER $$
DROP FUNCTION IF EXISTS `GetTimeDisplay2` $$
CREATE FUNCTION `GetTimeDisplay2` (GivenTimestamp TIMESTAMP)
RETURNS VARCHAR(32)
DETERMINISTIC
BEGIN
DECLARE rv VARCHAR(32);
DECLARE diff BIGINT;
SET diff = UNIX_TIMESTAMP()-UNIX_TIMESTAMP(GivenTimestamp);
IF diff < 0 THEN
SET rv = CONCAT(abs(diff/60),' From Now');
END IF;
IF diff = 0 THEN
SET rv = 'Just Now';
END IF;
IF diff = 1 THEN
SET rv = '1 sec ago';
END IF;
IF diff BETWEEN 2 AND 60 THEN
SET rv = CONCAT(FORMAT(diff, 0), ' secs ago');
END IF;
IF diff BETWEEN 120 AND 3599 THEN
SET rv = CONCAT(FORMAT(diff/60, 0), ' mins ago');
END IF;
IF diff BETWEEN 61 AND 119 THEN
SET rv = CONCAT(FORMAT(diff/60, 0), ' min ago');
END IF;
IF diff = 3600 THEN
SET rv = CONCAT(FORMAT(diff/3600, 0), ' hr ago');
END IF;
IF diff BETWEEN 3601 AND 86399 THEN
SET rv = CONCAT(FORMAT(diff/3600, 0), ' hrs ago');
END IF;
IF diff > 86400 THEN
SET rv = DATE_FORMAT(GivenTimestamp, '%a %l:%i %p');
END IF;
IF diff > 259200 THEN
SET rv = DATE_FORMAT(GivenTimestamp, '%b %e at %l:%i %p');
END IF;
RETURN rv;
END $$
DELIMITER ;
精彩评论