[英]PGError: ERROR: column “inboxes.id” must appear in the GROUP BY clause or be used in an aggregate function
I have the following query to MySQL database: 我对MySQL数据库有以下查询:
SELECT inboxes.*
, count(inboxes.conv) AS times
, max(created_at) AS created_at
FROM `inboxes`
WHERE to_user = 2
OR account_id = 2
GROUP BY conv
ORDER BY id DESC
This query works on my localhost, but if I deploy it to Heroku, I'll get this error: 该查询可在我的本地主机上运行,但是如果将其部署到Heroku,则会收到此错误:
PGError: ERROR: column "inboxes.id" must appear in the GROUP BY clause or
be used in an aggregate function
You need to specify all fields in GROUP BY
, which you wanna select, ie: 您需要在GROUP BY
指定要选择的所有字段,即:
SELECT inboxes.id, count(inboxes.conv) AS times, max(created_at) as created_at FROM
inboxes WHERE (to_user = 2 OR account_id = 2) GROUP BY inboxes.id, conv ORDER BY inboxes.id DESC
SELECT inboxes.id, count(inboxes.conv) AS times, max(created_at) as created_at FROM
WHERE (to_user = 2 OR account_id = 2) GROUP BY inboxes.id, conv ORDER BY inboxes.id DESC
为避免将来出现此类问题,请在本地安装postgres并使用与生产环境相同的数据库开发应用程序。
You should get rid of the inboxes.*
. 您应该摆脱inboxes.*
。 List each single parameter you need to fetch. 列出您需要提取的每个参数。
All parameters must be either grouped ( GROUP BY
) or used together with a group function( MAX
, etc.). 所有参数都必须分组( GROUP BY
)或与组函数一起使用( MAX
等)。
I cannot tell you why its working on your localhost. 我不能告诉你为什么它可以在你的本地主机上工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.