【发布时间】:2016-10-04 01:23:35
【问题描述】:
我有一个 Matchups 表,其中包含一个设备 ID 字段并存储一个数组
我正在尝试搜索 Matchups 表以获取任何具有包含特定设备 ID 的数组的记录。例如: 所以我跑了:
search_equipment_ids = [1, 2, 3, 4, 5]
Matchup.where("equipment_id <@ ARRAY[?]", search_equipment_ids)
以下是存储在设备ID表中的几个数组示例以及上述查询的结果
[1, 2] = True
[3, 5] = True
[1, 2, 3] = True
[1, 2, nil, 3] = False
如何让搜索忽略数组中可能包含 nil。我将更改数组的存储方式,因此从现在开始不会有 nil 值……但同时我需要考虑这些。
我使用:https://www.postgresql.org/docs/9.5/static/functions-array.html 作为参考。
我不想使用 && 运算符,因为 Matchups 表很大,如果仅比较是否存在一个设备 ID,它将返回许多结果。如果包含所有设备 ID(不包括任何 nil),我只想返回。
谢谢!
【问题讨论】: