簡體   English   中英

是否可以使用Rails 4查詢postgres數組的特定索引?

[英]Is it possible to query a specific index of a postgres array with Rails 4?

使用如下遷移的數據庫列:

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

我知道可以這樣做來查詢數組的任何元素:

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

我的問題是我是否可以專門查詢數組的第二個(或任何其他單個)元素?

您可以使用常用的數組索引表示法,但請記住,SQL數組是基於1的而不是基於0的:

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

精細的手冊有更多關於訪問數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM