从MySQL到PostgreSQL:如何修改此SQL查询? [英] MySQL to PostgreSQL: How to modify this SQL query?

查看:164
本文介绍了从MySQL到PostgreSQL:如何修改此SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个MySQL查询,它使用 MONTH() YEAR()

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

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

我将如何修改该查询以使其与PostgreSQL一起使用?

How would I modify that query to work with PostgreSQL?

推荐答案


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) 

这是最新的手册

http: //www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DA TETIME-EXTRACT

Btw:这也是在MySQL上也可以使用的标准(ANSI)SQL。

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

这篇关于从MySQL到PostgreSQL:如何修改此SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆