【问题标题】:How to fetch all rows where an array contains any of the fields array elements如何获取数组包含任何字段数组元素的所有行
【发布时间】:2022-01-20 10:54:32
【问题描述】:

我有一个表,其中有一列 video_ids,它属于 bigint[] 类型。我想找到在 select 语句中传递的数组中包含任何元素的所有行。因此,如果我有一行包含如下所示的 video_ids 字段:

{9529387, 9548200, 9579636}

如果我传递一个包含任何 video_ids 的数组,我想获取它。我以为我会用任何方法做到这一点,但我不确定如何在 SQL 中做到这一点,我已经尝试过:

select id, finished, failed, video_ids, invoiced_video_ids, failed_video_ids
from video_order_execution
where order_ids = any(
    '{9548200, 11934626, 9579636, 11936321, 11509698, 11552728, 11592106, 11643565, 11707543, 11810386, 11846268}'
        ::bigint[]);

如果我这样做会出错:

错误:运算符不存在:bigint[] = bigint 提示:没有运算符 匹配给定的名称和参数类型。您可能需要添加 显式类型转换。

我怎样才能做出这样的声明来满足我的需要?

【问题讨论】:

  • Edit问题并提供minimal reproducible example,即涉及的表或其他对象的CREATE语句(粘贴文本,不要使用图像,不要链接到外部站点),INSERT 用于示例数据 (dito) 的语句以及带有表格文本格式的示例数据的所需结果。标记您正在使用的 DBMS。

标签: sql postgresql where-clause array-intersect


【解决方案1】:

如果两个操作数有任何共同项,则使用运算符&& 返回true

select id, finished, failed, video_ids, invoiced_video_ids, failed_video_ids
from video_order_execution
where order_ids &&
  '{9548200, 11934626, 9579636, 11936321, 11509698, 11552728, 11592106, 11643565, 11707543, 11810386, 11846268}'::bigint[];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2015-01-08
    • 2013-04-18
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多