From Now() to Current_timestamp in Postgresql

后端 未结 5 1467
既然无缘
既然无缘 2021-01-30 19:31

In mysql I am able to do this:

SELECT *
FROM table
WHERE auth_user.lastactivity > NOW() - 100

now in postgresql I am using this query:

5条回答
  •  心在旅途
    2021-01-30 20:05

    Here is what the MySQL docs say about NOW():

    Returns the current date and time as a value in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.

    mysql> SELECT NOW();
            -> '2007-12-15 23:50:26'
    mysql> SELECT NOW() + 0;
            -> 20071215235026.000000
    

    Now, you can certainly reduce your smart date to something less...

    SELECT (
     date_part('year', NOW())::text
     || date_part('month', NOW())::text
     || date_part('day', NOW())::text
     || date_part('hour', NOW())::text
     || date_part('minute', NOW())::text
     || date_part('second', NOW())::text
    )::float8 + foo;
    

    But, that would be a really bad idea, what you need to understand is that times and dates are not stupid unformated numbers, they are their own type with their own set of functions and operators

    So the MySQL time essentially lets you treat NOW() as a dumber type, or it overrides + to make a presumption that I can't find in the MySQL docs. Eitherway, you probably want to look at the date and interval types in pg.

提交回复
热议问题