【问题标题】:Is it possible to query a specific index of a postgres array with Rails 4?是否可以使用 Rails 4 查询 postgres 数组的特定索引?
【发布时间】:2013-10-23 15:41:37
【问题描述】:

使用这样迁移的数据库列:

add_column :documents, :array_column, :string, array: true

我知道可以这样做来查询数组的任何元素:

Document.where("'foo' = ANY (array_column)")

我的问题是我是否可以专门查询数组的第二个(或任何其他单个)元素?

【问题讨论】:

    标签: ruby-on-rails arrays postgresql activerecord ruby-on-rails-4


    【解决方案1】:

    您可以使用通常的数组索引表示法,但请记住 SQL 数组是从 1 开始的,而不是从 0 开始的:

    Document.where("array_column[2] = 'foo'")
    

    精美的手册有更多关于accessing arrays的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      相关资源
      最近更新 更多