gpt4 book ai didi

sql - 根据 Postgres int 数组中存在的元素获取行

转载 作者:行者123 更新时间:2023-11-29 13:23:29 25 4
gpt4 key购买 nike

下面是我的 postgres 9.3 表

      id    arr
1 [1,2,3]
2 [1,2]
3 [1]
4 [2]
5 [4,5,6]

如果我搜索 1,2,我期望输出如下。我想要包含 1 或 2 的所有行。

  id    arr
1 [1,2,3]
2 [1,2]
3 [1]
4 [2]

我尝试使用下面的查询,即使用任何

  Select * from test where arr = ANY ('{1,2}'::int[])

报错

 ERROR: operator does not exist: integer[] = integer Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 30

我如何实现这一目标?

SQLFiddle - http://sqlfiddle.com/#!15/63c4f/4

最佳答案

使用the overlap operator :

Select * from test where arr && '{1,2}'::int[]

关于sql - 根据 Postgres int 数组中存在的元素获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37747066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com