4

在 PostgreSQL 上执行此查询时出现此错误:

SQLSTATE[42803]:分组错误:7 错误:列“posts.title”必须出现在 GROUP BY 子句中或在聚合函数中使用
第 1 行:SELECT DISTINCT posts.id, posts.* FROM "posts" GROUP BY "pos..

这是我的查询:

SELECT DISTINCT posts.id, posts.* 
FROM "posts" 
GROUP BY "posts"."id" 
ORDER BY "posts"."id" DESC 
LIMIT 20 OFFSET 0

虽然当我用mysql执行它时它确实有效,但我该如何解决这个问题?

4

2 回答 2

4

问题是,在 PostgreSQL 中,您需要将您希望在您的选择中包含的所有列添加到您的分组依据。

所以你的陈述应该是

SELECT * FROM table GROUP BY column1, column2...
于 2013-04-15T10:23:39.563 回答
-1

尝试这个。

SELECT DISTINCT * FROM 
(SELECT ROW_NUMBER() OVER(PARTITION BY  "posts"."id" ORDER BY   "posts"."id") No, posts.*
 FROM "posts"  ) AS T1 WHERE T1.NO = 1
 ORDER BY "id" DESC LIMIT 20 OFFSET 0
于 2013-04-15T10:27:10.953 回答