Mysql - selecting year from a unix timestamp

后端 未结 5 612
失恋的感觉
失恋的感觉 2020-12-15 07:07

I am using this:

SELECT FROM_UNIXTIME(my_unix_timestamp_column, \'%Y\') AS year FROM table_name WHERE year = 2009;

but it gives me an error

5条回答
  •  心在旅途
    2020-12-15 07:44

    SELECT  FROM_UNIXTIME(my_unix_timestamp_column, '%Y') AS `year`
    FROM    table_name
    HAVING  `year` = 2009
    

    Unlike WHERE clause, HAVING clause can reference the SELECT clause aliases.

    More index efficient way would be:

    SELECT  FROM_UNIXTIME(my_unix_timestamp_column, '%Y') AS `year`
    FROM    table_name
    WHERE   my_unix_timestamp_column >= UNIX_TIMESTAMP('2009-01-01')
            AND my_unix_timestamp_column < UNIX_TIMESTAMP('2010-01-01')
    

提交回复
热议问题