gpt4 book ai didi

sql - 标准 sql 查询以获取与另一个表匹配的记录字段(Google BigQuery)

转载 作者:行者123 更新时间:2023-12-04 09:45:52 25 4
gpt4 key购买 nike

我有两张 table

1) 表 main

   id    phone.type  phone.id
==============================
| 1 | android | adkfjagp |
| | android | asdfasdf |
| | iphone | akfj2341 |
| | iphone | ada93519 |
------------------------------

我有另一个表,它存储了一堆这样的安卓手机 ID

2) 表 android
==============
| adkfjagp |
| ... |
--------------

有没有一种方法可以获取表 main 中的所有行,其中该行包含一个类型为 android 和 id 的记录,该记录也在表 android 中。

最佳答案

下面应该可以



#standardSQL
SELECT m.*
FROM main AS m
CROSS JOIN (SELECT ARRAY_AGG(id) AS ids FROM android) AS a
WHERE (
SELECT COUNT(1)
FROM UNNEST(phone) AS phone
WHERE phone.type = 'android'
AND phone.id IN UNNEST(a.ids)
) > 0

您可以使用以下虚拟数据对其进行测试

#standardSQL
WITH main AS (
SELECT
1 AS id,
[STRUCT<type STRING, id STRING>
('android', 'adkfjagp'),
('android', 'asdfasdf'),
('iphone', 'akfj2341'),
('iphone', 'ada93519')
] AS phone UNION ALL
SELECT
2 AS id,
[STRUCT<type STRING, id STRING>
('android', 'adkfjagp1'),
('android', 'bbbbbbbb1'),
('android', 'akfj2341'),
('iphone', 'ada93519')
] AS phone
),
android AS (
SELECT 'adkfjagp' AS id UNION ALL
SELECT 'bbbbbbbb'
)
SELECT m.*
FROM main AS m
CROSS JOIN (SELECT ARRAY_AGG(id) AS ids FROM android) AS a
WHERE (
SELECT COUNT(1)
FROM UNNEST(phone) AS phone
WHERE phone.type = 'android'
AND phone.id IN UNNEST(a.ids)
) > 0

关于sql - 标准 sql 查询以获取与另一个表匹配的记录字段(Google BigQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43006736/

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