简体   繁体   中英

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

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 FROM inboxes 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.* . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM