【问题标题】:Pure SQL Query in Rails to get customers in last 30 daysRails 中的纯 SQL 查询在过去 30 天内获得客户
【发布时间】:2016-02-09 13:37:25
【问题描述】:

我通常只会使用:

ShopifyAPI::Customer.where("created_at > ?", 30.days.ago).count

但是我收到了这个错误

ArgumentError:参数数量错误(2 代表 0..1)

所以我正在尝试编写一个纯 SQL 查询:

ShopifyAPI::Customer.where("created_at > DATEADD(day, -30, getdate())").count

但是得到这个错误:

ArgumentError: 期望子句哈希,得到“created_at > DATEADD(day, -30,getdate())"

关于如何获取过去 30 天的客户数量的任何想法?

【问题讨论】:

  • 底层数据库是什么?请用它标记问题。
  • 对不起-psql

标签: sql ruby-on-rails postgresql shopify


【解决方案1】:

只需编辑您的第一个代码,如下所示:

ShopifyAPI::Customer.where(["created_at > ?", 30.days.ago]).count

【讨论】:

    【解决方案2】:

    在 Postgres 中,where 子句看起来像:

    ShopifyAPI::Customer.where("created_at > CURRENT_DATE - interval '30 day' ").count
    

    您使用的语法是 SQL Server 语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 2011-08-24
      • 2016-06-17
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2020-07-27
      • 2019-01-14
      相关资源
      最近更新 更多