列" users.id"必须出现在GROUP BY子句或聚合函数中使用 [英] column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
本文介绍了列" users.id"必须出现在GROUP BY子句或聚合函数中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关系:
Item belongs to Product
Product belongs to User
产品型号范围:
Item model scope:
scope :search, ->(search_term) {
select('products.name, users.*, products.brand, COUNT(products.id)')
.joins(:product => :user)
.where('users.name = ? OR products.brand = ?', search_term, search_term)
.group('products.id')
}
在下面的SQL语句以上的结果:
The above results in the following SQL statement:
SELECT products.name, users.*, products.brand, COUNT(products.id) FROM "items"
INNER JOIN "products" ON "products"."id" = "items"."product_id"
INNER JOIN "users" ON "users"."id" = "products"."user_id"
WHERE (users.name = 'Atsuete Lipstick' OR products.brand = 'Atsuete Lipstick')
GROUP BY products.id
这里的问题是,出现了一个错误:
The problem here is that an error occurs:
ActiveRecord::StatementInvalid: PG::Error: ERROR: column "users.id"
must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT products.name, users.*, products.brand, COUNT(product...
什么能修复这个?
What could be a fix for this?
推荐答案
从错误你可以看到,你应该尝试在<$ C $包括 users.id
C> GROUP BY 子句:
From the error you can see that you should try including users.id
in the GROUP BY
clause:
.group('products.id, users.id')
这篇关于列&QUOT; users.id&QUOT;必须出现在GROUP BY子句或聚合函数中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文