简体   繁体   English

pgSQL查询错误

[英]pgSQL query error

i tried using this query: 我尝试使用此查询:

"SELECT * FROM guests WHERE event_id=".$id." GROUP BY member_id;" “SELECT * FROM guests WHERE event_id =”。$ id。“GROUP BY member_id;”

and I'm getting this error: 我收到这个错误:

ERROR: column "guests.id" must appear in the GROUP BY clause or be used in an aggregate function 错误:列“guests.id”必须出现在GROUP BY子句中或用于聚合函数

can anyone explain how i can work around this? 任何人都可以解释我如何解决这个问题?

You can't Group By without letting the Select know what to take, and how to group. 您不能在不让Select知道要采取什么以及如何分组的情况下进行分组。
Try 尝试

SELECT guests.member_id FROM guests WHERE event_id=".$id." GROUP BY member_id;

IF you need to get more info from this table about the guests, you'll need to add it to the Group By. 如果您需要从此表中获取有关来宾的更多信息,则需要将其添加到“分组依据”中。

Plus, it seems like your select should actually be 另外,看起来您的选择实际上应该是

SELECT guests.id FROM guests WHERE event_id=".$id." GROUP BY id;

Each of the columns used in a group by query needs to be specifically called out (ie, don't do SELECT * FROM ... ), as you need to use them in some sort of aggregate function (min/max/sum/avg/count/etc) or be part of the group by clause. 需要专门调出一group by查询中使用的每个列(即,不要执行SELECT * FROM ... ),因为您需要在某种聚合函数中使用它们(min / max / sum / avg / count / etc)或成为group by子句的一部分。

For example: 例如:

SELECT instrument, detector, min(date_obs), max(date_obs)
FROM observations
WHERE observatory='SOHO'
GROUP BY instrument, detector;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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