在JO语句中使用OR的SQL JOIN

时间:2014-10-31 19:49:51

标签: sql google-bigquery

我在Google BigQuery上运行SQL查询并希望执行此类SQL命令:

SELECT ... FROM A JOIN B
ON A.col1=B.col1 AND (A.col2=B.col2 OR A.col3=B.col3)

虽然出现错误但失败了:

Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name.

有没有办法重写SQL以获得这种功能?

2 个答案:

答案 0 :(得分:2)

结果证明这是有效的,相当于Google BigQuery中的UNION ALL语句。如果你只想要一个UNION,不知道怎么做,因为BigQuery实际上不支持DISTINCT。幸运的是,这对我来说已经足够了。

SELECT ... FROM 

(SELECT ... FROM A JOIN B ON A.col1=B.col1 AND A.col2=B.col2),
(SELECT ... FROM A JOIN B ON A.col1=B.col1 AND A.col3=B.col3)

答案 1 :(得分:1)

这应该有效:

SELECT ... FROM A CROSS JOIN B
WHERE A.col1=B.col1 AND (A.col2=B.col2 OR A.col3=B.col3)