我想知道如何在Google BigQuery中编写类似于以下针对MySQL的多表联接:
SELECT t2.*, t1.*, t3.* FROM table1 t1, table2 t2, table t3 WHERE t1.field_a=t2.field_a AND t2.field_b=t3.field_b AND t1.field_condition=10
请注意,我请求表的顺序为特定顺序,但使用星号功能。
有什么想法吗?
答案 0 :(得分:1)
当列与名称匹配时(如在问题中),较短的一个:
#standardSQL
WITH t_a AS (SELECT 1 id_a, 2 v)
, t_b AS (SELECT 1 id_a, 2 id_c, 3 w)
, t_c AS (SELECT 2 id_c, 5 z)
SELECT *
FROM t_a
JOIN t_b USING(id_a)
JOIN t_c USING(id_c)
答案 1 :(得分:0)
以下是用于BigQuery标准SQL
#standardSQL
SELECT t2.*, t1.*, t3.*
FROM table1 t1
JOIN table2 t2 ON t1.field_a=t2.field_a
JOIN table t3 ON t2.field_b=t3.field_b
WHERE t1.field_condition=10
以上假设字段名称中没有歧义
同时,以下内容仍适用于BigQuery Standard SQL
SELECT t2.*, t1.*, t3.* FROM table1 t1, table2 t2, table t3 WHERE t1.field_a=t2.field_a AND t2.field_b=t3.field_b AND t1.field_condition=10
并等于
#standardSQL
SELECT t2.*, t1.*, t3.* FROM table1 t1
CROSS JOIN table2 t2
CROSS JOIN table t3
WHERE t1.field_a=t2.field_a AND t2.field_b=t3.field_b AND t1.field_condition=10