【发布时间】:2015-01-16 15:36:00
【问题描述】:
我的 ActiveRecord 查询有一个小问题。
我有一个 Product 模型,它看起来像:
#<Product id: nil, name: nil, price: nil, order_id: nil, created_at: nil, updated_at: nil, restaurant_id: nil>
现在我想在所有名称上选择 DISTINCT,并取回所有产品的属性。
我试过了:
@products = Product.where(restaurant_id: !nil).group("products.name").order("name")
但我收到了这个错误:
PG::GroupingError: ERROR: column "products.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "products".* FROM "products" WHERE "products"."rest...
^
: SELECT "products".* FROM "products" WHERE "products"."restaurant_id" = 1 GROUP BY products.name
好的,然后我将 product_id 添加到我的查询中:
@products = Product.where(restaurant_id: !nil).group("products.name, products.id").order("name")
但此查询返回名称重复的产品。 所以我也尝试了这个:
@products = Product.where(restaurant_id: !nil).select("DISTINCT(NAME)").order("name")
但作为回报,我只得到了带有 id 和 name 的 Product 记录(很明显),所以如果这个查询返回正确的集合,我添加了稍后需要的属性:
@products = Product.where(restaurant_id: !nil).select("DISTINCT(NAME), restaurant_id, price").order("name")
它还会返回重复的名称。
您有什么解决方案或想法,如何为 PostgreSQL 修复此查询?
我习惯于写这样的查询(在 MySQL 上),这是正确的:
@products = Product.where(restaurant_id: !nil).select("DISTINCT(NAME), restaurant_id, price").order("name")
为什么 PostreSQL 不接受该查询?
【问题讨论】:
-
试试这个
Product.where.not(restaurant_id: nil).select("DISTINCT ON(name) name, restaurant_id, price, updated_at").order("name, updated_at")。阅读SELECT DISTINCT ON ( expression [, ...] )只保留给定表达式计算结果为等于的每组行的第一行...... -
请告诉我结果。我无法尝试,因为我没有准备好数据的表格。所以我需要你的确认..)
标签: ruby-on-rails ruby postgresql ruby-on-rails-4 activerecord