gpt4 book ai didi

sql - MySQL 到 PostgreSQL : How to modify this SQL query?

转载 作者:行者123 更新时间:2023-11-29 01:24:54 24 4
gpt4 key购买 nike

我有一个使用 MONTH()YEAR() 的 MySQL 查询:

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 配合使用?

最佳答案

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-DATETIME-EXTRACT

顺便说一句:这是标准 (ANSI) SQL,也适用于 MySQL。

关于sql - MySQL 到 PostgreSQL : How to modify this SQL query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4493705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com