简体   繁体   English

如何内部连接两个表?

[英]How can I inner join two tables?

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

Hi !你好 ! Anyone can tell me what's wrong with my jointure?谁能告诉我我的关节有什么问题? Thank you very much!非常感谢!

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;

Refer columns with alias of tables:引用具有表别名的列:

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;

Try this :尝试这个 :

  1. You have to add alias for table & use into select column statements and join.您必须为表和使用添加别名到选择列语句和连接中。

When you run the query, the error message is pretty clear:运行查询时,错误消息非常清楚:

Error: Column name country_code is ambiguous at [2:1]错误:列名 country_code 在 [2:1] 处不明确

You should learn to read the error messages;你应该学会阅读错误信息; BQ is pretty good at explaining issues in queries. BQ 非常擅长解释查询中的问题。

The column country_code is in both tables. country_code列在两个表中。 As mentioned in other answers, you want to qualify all column references.正如其他答案中提到的,您希望限定所有列引用。 I want to point out that simpler table aliases makes the query easier to write and read.我想指出,更简单的表别名使查询更易于编写和阅读。

You can also use USING , because the JOIN keys have the same name:您也可以使用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