PostgreSQL分组错误 [英] PostgreSQL Grouping error

查看:242
本文介绍了PostgreSQL分组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PostgreSQL上执行此查询时遇到此错误:

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

这是我的查询:

  SELECT DISTINCT帖子.id,posts。* 
来自 posts
GROUP BY posts。 id
ORDER BY posts。 id DESC
LIMIT 20 OFFSET 0

尽管在使用mysql执行该命令时它确实起作用了,所以如何解决呢?

解决方案

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

所以您的陈述应该像

  SELECT * FROM table GROUP BY column1 ,第2列... 


I got this error when i was executing this query on PostgreSQL:

SQLSTATE[42803]: Grouping error: 7 ERROR:  column "posts.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT DISTINCT posts.id, posts.* FROM "posts" GROUP BY "pos..

and this is my query:

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

although it did worked when I was executing it with mysql, so how can i fix this?

解决方案

The problem is, that in PostgreSQL you need to add all the columns you want to have in your select to your group by.

so your statement should be like

SELECT * FROM table GROUP BY column1, column2...

这篇关于PostgreSQL分组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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