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

最佳答案

您需要在GROUP BY中指定要选择的所有字段,即:
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

关于mysql - PGError:错误:列“inboxes.id”必须出现在GROUP BY子句中或在聚合函数中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8204789/

10-09 12:33