使用有效记录的最小值或最大值

时间:2016-03-26 10:48:43

标签: sql ruby-on-rails postgresql activerecord

我试图通过以下查询从我们的数据库中获取最小值:

@supplier = Supplier.joins(:products).where('suppliers.permalink = ?', params[:id]).group('suppliers.id','products.id','products.name',min(variants.price)).pluck('products.id','products.name','suppliers.id',min(variants.price))

当我运行我认为查询应该是的时候,我得到了这个结果:

SELECT suppliers.id as supp_id, products.id as prod_id, products.name as prod_name, min(price) as low_price, max(price) as high_price from suppliers
  INNER JOIN variant_suppliers on variant_suppliers.supplier_id = suppliers.id
  INNER JOIN variants on variants.id = variant_suppliers.variant_id
  INNER JOIN products on products.id = variants.product_id
WHERE suppliers.id in (1,2)
GROUP BY suppliers.id, products.name, products.id;

0 个答案:

没有答案