繁体   English   中英

如何内部连接两个表?

[英]How can I inner join two tables?

在此处输入图片说明 在此处输入图片说明

你好 ! 谁能告诉我我的关节有什么问题? 非常感谢!

SELECT 
country_code,
country_name,
year,
crude_birth_rate,
crude_death_rate

FROM `bigquery-public-data.census_bureau_international.country_names_area`as Country_name_area
INNER JOIN `bigquery-public-data.census_bureau_international.birth_death_growth_rates` as birth_death_growth_rates

ON birth_death_growth_rates.country_code = Country_name_area.country_code;

引用具有表别名的列:

SELECT 
Country_name_area.country_code,
Country_name_area.country_name,
birth_death_growth_rates.year,
birth_death_growth_rates.crude_birth_rate,
birth_death_growth_rates.crude_death_rate

FROM `bigquery-public-data.census_bureau_international.country_names_area`as Country_name_area
INNER JOIN `bigquery-public-data.census_bureau_international.birth_death_growth_rates` as birth_death_growth_rates

ON birth_death_growth_rates.country_code = Country_name_area.country_code;

尝试这个 :

  1. 您必须为表和使用添加别名到选择列语句和连接中。

运行查询时,错误消息非常清楚:

错误:列名 country_code 在 [2:1] 处不明确

你应该学会阅读错误信息; BQ 非常擅长解释查询中的问题。

country_code列在两个表中。 正如其他答案中提到的,您希望限定所有列引用。 我想指出,更简单的表别名使查询更易于编写和阅读。

您也可以使用USING ,因为JOIN键具有相同的名称:

SELECT country_code, cna.country_name, bdgr.year,
       bdgr.crude_birth_rate, bdgr.crude_death_rate
FROM `bigquery-public-data.census_bureau_international.country_names_area` cna JOIN
     `bigquery-public-data.census_bureau_international.birth_death_growth_rates` bdgr
     USING (country_code);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM