在 BigQuery 中使用 exists 进行过滤

Filtering with exists in BigQuery

我是 运行 bigQuery 中的以下查询,但没有得到预期的输出。不是应该只返回带有 Alpha 的行吗?

SELECT * FROM UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta')
])
WHERE EXISTS (SELECT * FROM UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha')
]))

改用下面的方法

SELECT * FROM UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta')
])
WHERE (a,b) in UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha')
])            

有输出

您忘记了 EXISTS 子查询中的 WHERE 子句,这就是为什么它总是 return 行并计算为 true.

试试这个:

SELECT * FROM UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta')
]) t1
WHERE EXISTS (
    SELECT 1 FROM UNNEST([
        STRUCT(NULL AS a, '' AS b),
              (1, 'Alpha')
       ]) t2
    WHERE t1.a = t2.a -- add this condition
)