Mysql - selecting year from a unix timestamp

后端 未结 5 596
失恋的感觉
失恋的感觉 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:33

    Another alternative, avoiding repetition of a biggish function call:

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

    You might still need to use back-quotes around the word 'year' to avoid conflicts with YEAR as a keyword. The optimizer should not need to create an intermediate table to answer this query.

提交回复
热议问题