0

與數據庫列遷移這樣的:是否有可能使用Rails 4查詢postgres數組的特定索引?

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

我知道這是可能做到這一點查詢數組中的任何元素:

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

我的問題是我是否能具體查詢第二個(或任何其他單個)元素的數組?

回答

2

可以使用通常的數組索引符號但要記住,SQL陣列爲基礎的1,而不是0爲基礎的:

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

細手冊有更多關於accessing arrays

相關問題