gpt4 book ai didi

postgresql - 列必须出现在 GROUP BY 子句中

转载 作者:行者123 更新时间:2023-11-29 12:30:44 28 4
gpt4 key购买 nike

我有这个问题:

SELECT 
"EventReadingListItem"."id"
, "EventReadingListItem"."UserId"
FROM "EventReadingListItems" AS "EventReadingListItem"
group by "EventReadingListItem"."EventId";

运行时报错

Column "EventReadingListItem"."id" must appear in the GROUP BY clause or be used in an aggregate function.

为什么?我读过类似的问题,但我真的不明白为什么这个简单的 group by 不起作用。是因为 group by 中的字段还未被称为“EventReadingListItem”吗?

最佳答案

因此,根据您的评论,这应该适合您。

为每个具有最小/最小 id 值的 EventId 提供唯一的行:

select DISTINCT ON (EventId) EventId, id, UserId
from EventReadingListItems
order by EventId, id

关于postgresql - 列必须出现在 GROUP BY 子句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900414/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com