MySQL to PostgreSQL: How to modify this SQL query?

前端 未结 2 1335
借酒劲吻你
借酒劲吻你 2021-01-14 21:14

I have this MySQL query that makes use of MONTH() and YEAR():

SELECT 
  MONTH(created_at) AS month, 
  YEAR(created_at) AS year 
FR         


        
相关标签:
2条回答
  • 2021-01-14 21:34

    http://www.postgresql.org/docs/9.0/static/functions-datetime.html

    SELECT 
      EXTRACT(MONTH FROM created_at) AS month, 
      EXTRACT(YEAR FROM created_at) AS year 
    FROM users 
    GROUP BY EXTRACT(MONTH FROM created_at), EXTRACT(YEAR FROM created_at)
    ORDER BY EXTRACT(YEAR FROM created_at), EXTRACT(MONTH FROM created_at)
    

    This syntax should work on PostgreSQL, Oracle and Teradata

    0 讨论(0)
  • 2021-01-14 21:37
    SELECT 
      extract(MONTH from created_at) AS month, 
      extract(YEAR from created_at) AS year 
    FROM users 
    GROUP BY extract(MONTH from created_at), extract(YEAR from created_at) 
    ORDER BY extract(MONTH from created_at), extract(YEAR from created_at) 
    

    Here is the up-to-date manual
    http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

    Btw: This is standard (ANSI) SQL that works on MySQL as well.

    0 讨论(0)
提交回复
热议问题