在MySQL中计算Timespan

时间:2011-10-05 22:33:01

标签: mysql

我正在尝试查询其中包含日期的表。我想从表中取出日期并将其与当前时间进行比较。我希望看到类似的东西。

2011.10.05 10:12:50 - Date time table
Current date

我需要它说1 day 14 mins 13 secs

我考虑在查询中使用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 ;

1 个答案:

答案 0 :(得分:1)

此查询将显示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美国东部时间

我写了一个你可以调用的存储函数,它会为你处理这个:

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";

您可能希望将存储的函数移动到另一个数据库。我的代码将存储的函数放在测试数据库中。

试一试!!!