PGError:错误:列“inboxes.id”必须出现在 GROUP BY 子句中或在聚合函数中使用

发布于 12-17 00:47 字数 423 浏览 4 评论 0原文

我对 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

此查询在我的本地主机上运行,​​但如果我将其部署到 Heroku,我会收到此错误:

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:

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:

PGError: ERROR:  column "inboxes.id" must appear in the GROUP BY clause or
                 be used in an aggregate function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有一腔孤勇2024-12-24 00:47:55

您应该删除收件箱。*。列出您需要获取的每个参数。

所有参数必须进行分组(GROUP BY)或与分组函数(MAX 等)一起使用。

我无法告诉你为什么它在你的本地主机上工作。

You should get rid of the 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.).

I cannot tell you why its working on your localhost.

秋凉2024-12-24 00:47:54

您需要指定 GROUP BY 中要选择的所有字段,即:

SELECT inboxes.id, count(inboxes.conv) AS times, max(created_at) ascreated_at FROM收件箱<代码>WHERE(to_user = 2 OR account_id = 2)GROUP BY inboxes.id,conv ORDER BY inboxes.id DESC

You need to specify all fields in GROUP BY, which you wanna select, ie:

SELECT inboxes.id, count(inboxes.conv) AS times, max(created_at) as created_at FROMinboxesWHERE (to_user = 2 OR account_id = 2) GROUP BY inboxes.id, conv ORDER BY inboxes.id DESC

江心雾2024-12-24 00:47:54

为了避免将来出现类似问题,请在本地安装 postgres 并使用与生产中使用的相同数据库来开发应用程序。

To avoid problems like this in the future, install postgres locally and develop your application with the same database it is using in production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文