【问题标题】:Rewrite sql statement in ruby active records重写 ruby​​ 活动记录中的 sql 语句
【发布时间】:2017-09-14 20:45:51
【问题描述】:

假设我有

class KdpReport < ActiveRecord::Base
end

如何使用活动记录模型在 ruby​​ 中翻译这条 sql 语句?

 SELECT DISTINCT
        `kdp_reports`.`date` AS `date`,
        `kdp_reports`.`book_title` AS `book_title`,
        `kdp_reports`.`marketplace` AS `marketplace`
 FROM
    `kdp_reports`

我使用红宝石“2.4.0” 和没有轨道的活动记录宝石

我只能想到这样的事情:

KdpReport.select(:date).distinct.select(:book_title).distinct.select(:marketplace).distinct

【问题讨论】:

  • 您提出的解决方案完全符合您的要求。问题是什么?请停止在 StackOverflow 上发帖。

标签: sql ruby activerecord


【解决方案1】:
KdpReport.select('date, book_title, marketplace').distinct

参考见:

您还可以将值作为符号数组传递,或使用uniq 作为distinct 的别名,即

KdpReport.select([:date, :book_title, :marketplace]).uniq

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2022-01-27
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多