我正在尝试查询其中包含日期的表格。我想从表中取出日期并将其与当前时间进行比较。我希望看到这样的东西。
2011.10.05 10:12:50 - Date time table
Current date
我需要它说 1 天 14 分 13 秒
。
我想过在查询中使用 datesub()
但它没有给我想要的东西。有没有办法在查询中完成这个,如果没有我需要走另一条路。我什至通读了手册,但找不到任何关于时间跨度的信息。
$query = "select country, rprice as regPrice, mprice as midPrice, pprice as prePrice, saddress as streetAddress,
_id as ID, lat, lng, sname as Name, logo, admin_level_1 as state, locale as city, rdate as regDate,
mdate as midDate, pdate as preDate,
format((acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) *
cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378),1) as distance from stationDetails where
(acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * cos(radians(lat)) *
cos(radians($lng1) - radians(lng))) * 6378) <= $rad order by $sort asc, $type asc";
此查询有效,但我需要获取 rdate、mdate 和 pdate 并将其转换为时间,因为它已在数据库中更新。 @RolandoMySQLDBA 查询工作得很好,就像我想要的那样,但是由于某种原因,当我将它放入上面的查询中时,它中断了,并告诉我我的 SQL 语法中有。
编辑:这是我在调整一些东西并学习如何编写函数后想出的。
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 ;
最佳答案
此查询将显示从 2011 年 2 月 1 日午夜开始的确切天数、小时数、分钟数和秒数:
SELECT
TRIM(REPLACE(CONCAT(
IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs '))),
IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),' ',' '))
TimeDisplay
FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
FROM (SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2011-02-01 00:00:00')) sec_a)
A) AA) AAA) AAAA) B;
只需将 '2011-02-01 00:00:00'
替换为您想要的任何日期时间值或表列名称即可。
试一试!!!
更新 2011-10-06 13:38 EDT
我写了一个存储函数,你可以调用它来为你处理:
DELIMITER $$
DROP FUNCTION IF EXISTS `test`.`GetTimeDisplay` $$
CREATE FUNCTION `test`.`GetTimeDisplay` (GivenTimestamp TIMESTAMP)
RETURNS VARCHAR(32)
DETERMINISTIC
BEGIN
DECLARE rv VARCHAR(32);
DECLARE diff BIGINT;
SET diff = UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp);
SELECT
TRIM(REPLACE(CONCAT(
IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs '))),
IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),' ',' '))
INTO rv
FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
FROM (SELECT ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp)) sec_a)
A) AA) AAA) AAAA) B;
IF diff = 0 THEN
SET rv = '0 secs';
END IF;
IF diff < 0 THEN
SET rv = CONCAT(rv,' From Now');
END IF;
IF diff > 0 THEN
SET rv = CONCAT(rv,' Ago');
END IF;
RETURN rv;
END $$
DELIMITER ;
您可以像这样重写查询:
$query = "select country, rprice as regPrice, mprice as midPrice, pprice as prePrice, saddress as streetAddress,
_id as ID, lat, lng, sname as Name, logo, admin_level_1 as state, locale as city, test.GetTimeDisplay(rdate) as regDate,
test.GetTimeDisplay(mdate) as midDate, test.GetTimeDisplay(pdate) as preDate,
format((acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) *
cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378),1) as distance from stationDetails where
(acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * cos(radians(lat)) *
cos(radians($lng1) - radians(lng))) * 6378) <= $rad order by $sort asc, $type asc";
您可能希望将存储的函数移动到另一个数据库。我的代码将存储的函数放在测试数据库中。
试一试!!!
关于mysql - 在 MySQL 中计算时间跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7668537/