我想在大查询中加入两个具有公共列的表?
Posted
技术标签:
【中文标题】我想在大查询中加入两个具有公共列的表?【英文标题】:I want to join two tables with a common column in Big query? 【发布时间】:2018-01-09 19:42:03 【问题描述】:要加入表格,我使用以下查询。
SELECT *
FROM(select user as uservalue1 FROM [projectname.FullData_Edited]) as FullData_Edited
JOIN (select user as uservalue2 FROM [projectname.InstallDate]) as InstallDate
ON FullData_Edited.uservalue1=InstallDate.uservalue2;
查询有效,但连接表只有两列 uservalue1 和 uservalue2。 我想保留两个表中的所有列。知道如何实现吗?
【问题讨论】:
【参考方案1】:#legacySQL
SELECT <list of fields to output>
FROM [projectname:datasetname.FullData_Edited] AS FullData_Edited
JOIN [projectname:datasetname.InstallDate] AS InstallDate
ON FullData_Edited.user = InstallDate.user
或(更可取的)
#standardSQL
SELECT <list of fields to output>
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
ON FullData_Edited.user = InstallDate.user
注意,在这种情况下使用SELECT *
会导致Ambiguous column name
错误,因此最好在输出中明确列出您需要的列/字段
解决方法是使用USING()
语法,如下例所示。
假设 user
是唯一的模棱两可的字段 - 它可以解决问题
#standardSQL
SELECT *
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
USING (user)
例如:
#standardSQL
WITH `projectname.datasetname.FullData_Edited` AS (
SELECT 1 user, 'a' field1
),
`projectname.datasetname.InstallDate` AS (
SELECT 1 user, 'b' field2
)
SELECT *
FROM `projectname.datasetname.FullData_Edited` AS FullData_Edited
JOIN `projectname.datasetname.InstallDate` AS InstallDate
USING (user)
返回
user field1 field2
1 a b
而使用ON FullData_Edited.user = InstallDate.user
会出现以下错误
Error: Duplicate column names in the result are not supported. Found duplicate(s): user
【讨论】:
如果它不是唯一的模棱两可的文件呢?【参考方案2】:如果您想要所有列,请不要使用子查询:
SELECT *
FROM [projectname.FullData_Edited] as FullData_Edited JOIN
[projectname.InstallDate] as InstallDate
ON FullData_Edited.uservalue1 = InstallDate.uservalue2;
您可能必须列出要避免列名重复的特定列。
当您使用它时,您还应该切换到标准 SQL。
【讨论】:
@VSR 。 . .请注意答案中的部分内容:“您可能必须列出要避免列名重复的特定列。”以上是关于我想在大查询中加入两个具有公共列的表?的主要内容,如果未能解决你的问题,请参考以下文章