简体   繁体   English

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

[英]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.

相关问题 SQL:列必须出现在 GROUP BY 子句中或在聚合函数中使用 - SQL: Column Must Appear in the GROUP BY Clause Or Be Used in an Aggregate Function postgres-SELECT / GROUP BY中的部分列-列必须出现在GROUP BY子句中或在聚合函数中使用 - postgres - partial column in SELECT/GROUP BY - column must appear in the GROUP BY clause or be used in an aggregate function 无效操作:列“[column_name]”必须出现在 GROUP BY 子句中或在聚合 function 中使用; - Invalid operation: column “[column_name]” must appear in the GROUP BY clause or be used in an aggregate function; 必须出现在GROUP BY子句中或在聚合函数中使用(PostgreSQL) - Must appear in the GROUP BY clause or be used in an aggregate function (PostgreSQL) 列“d.discount_amount”必须出现在 GROUP BY 子句中或用于聚合函数中 - column “d.discount_amount” must appear in the GROUP BY clause or be used in an aggregate function SQL:必须出现在GROUP BY子句中或在聚合函数中使用 - SQL: Must appear in the GROUP BY clause or be used in an aggregate function GROUP BY子句必须与聚合函数一起使用吗? - The GROUP BY clause must be used with aggregate functions? 选定的项目不必出现在 GROUP BY 子句中或用于聚合 function - selected items don't have to appear in the GROUP BY clause or be used in an aggregate function 当 select 列不是聚合 function 也不在 group by 子句中时不会出现错误 - Not getting an error when select a column that is not an aggregate function nor in the group by clause invalid:聚合函数或GROUP BY子句 - invalid : aggregate function or the GROUP BY clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM