首页 > 解决方案 > 如何在 where 子句 [BigQuery] 中为数组设置条件

问题描述

我的桌子上有一个数组 - 像这样:

在此处输入图像描述

我只需要考虑 'top_authors.author' = 'Caivi' 和 'top_authors.total_score' = 3 的行

我试图使用 unnest 函数,但我仍然收到错误“没有匹配的运算符 = 参数类型的签名:ARRAY,STRING。支持的签名:ANY = ANY”

你能帮我解决这个问题吗?

标签: sqlgoogle-cloud-platformgoogle-bigquery

解决方案


您可以unnest()where子句中的子查询中:

where exists (select 1
              from unnest(top_authors) ta
              where ta.author = 'Caivi' and ta.total_score = 3
             )

或者您可以在主查询中执行此操作:

select . . . 
from t cross join
     unnest(top_authors) ta
where ta.author = 'Caivi' and ta.total_score = 3;

假设您在数组中没有重复项,这些应该会产生等效的结果。


推荐阅读