postgresql 在rails psql Query中查询JSON数组

a0x5cqrl  于 2022-11-04  发布在  PostgreSQL
关注(0)|答案(2)|浏览(151)

我有JSON

a = {
    cake: ["a","b","c","d"]
}

我需要查询任意一个值在给定的数组中是否存在值Like如果输入是["a","x"]输出应该是true

if input is ["c","d"] then output is true

if input is ["x","z"] the n output is false
dtcbnfnu

dtcbnfnu1#

使用交集(&)并检查原始数组中是否有任何输入:

2.6.6 :002 > (a[:cake] & ['a','x']).any?
 => true 

2.6.6 :003 > (a[:cake] & ['x','z']).any?
 => false
yb3bgrhw

yb3bgrhw2#

只需尝试找到json数组和input数组之间公共点。

(a[:cake] & input_array).any?

相关问题