简体   繁体   English

尝试从两个不同的表中加入列

[英]Trying to JOIN columns from two different tables

I have one table (city_data) with the columns 'year', 'city_name' and 'avg_temp'.我有一张表(city_data),其中包含“year”、“city_name”和“avg_temp”列。 The other table is global_data and has the columns 'avg_temp' and 'year'.另一个表是 global_data 并具有列“avg_temp”和“year”。 I am trying to create a table which has the average temperature of London, the global average temperature for all the years.我正在尝试创建一个具有伦敦平均温度的表格,即多年来的全球平均温度。 My SQL code is the following:我的 SQL 代码如下:

SELECT city_data.avg_temp, city_data.year
FROM city_data
JOIN global_data
ON city_data.year = global_data.avg_temp
WHERE city= 'Paris'

I get no error but cannot get the expected result.我没有收到错误,但无法获得预期的结果。 I think I am not able to understand what I need to put in the ON clause.我想我无法理解我需要在 ON 子句中添加什么。

FROM city_data
JOIN global_data
ON city_data.year = global_data.year

-- it should be global_data.year in Join -- Join 中应该是global_data.year

WHERE city= 'Paris'

This is completely based on the partial understanding这完全基于部分理解

SELECT city_name, AVG(city_data.avg_temp) as global_avg
    FROM
    city_data
    JOIN global_data
    ON city_data.year = global_data.year
    WHERE city_name = 'Paris'
    GROUP BY city_data.city_name

As year is the only common attribute, join using it.由于year是唯一的共同属性,因此使用它进行连接。 Then fetch the city average table as it is readily availble in the city_data .然后获取城市平均表,因为它在city_data中很容易获得。 Lastly do the avg for global_data , as you want to fetch it across years.最后做global_data的 avg ,因为你想跨年获取它。

SELECT `city_name`, `city_data`.`avg_temp` AS city_avg_temp, AVG(`global_data`.`avg_temp`) AS global_avg_temp
    FROM `city_data`
    JOIN `global_data` 
        ON `city_data`.`year` = `global_data`.`year`
    WHERE `city_name` = 'London'
    GROUP BY `city_data`.`city_name`

I suppose that the table global_data has one row per year, while city_data has one row per year and city.我想表global_data每年有一行,而city_data每年有一行,城市。 This means you can simply join the two on the year they have in common:这意味着您可以简单地在他们共同的年份加入两者:

select year, g.avg_temp as global_average, c.avg_temp as paris_average
from global_data g
join city_data c using (year)
where c.city = 'Paris'
order by year;

If you don't like to join the two and only then mention Paris, you can also turn this into:如果你不喜欢加入两者然后才提到巴黎,你也可以把它变成:

select year, g.avg_temp as global_average, p.avg_temp as paris_average
from global_data g
join (select * from city_data where city = 'Paris') p using (year)
order by year;

As of MySQL 8 you can even use a WITH clause to "prepare" Paris:-)从 MySQL 8 开始,您甚至可以使用WITH子句来“准备”巴黎:-)

with paris as (select * from city_data where city = 'Paris')
select year, g.avg_temp as global_average, paris.avg_temp as paris_average
from global_data g
join paris using (year)
order by year;

Or consider Paris a join criteria:或将巴黎视为加入标准:

select g.year, g.avg_temp as global_average, p.avg_temp as paris_average
from global_data g
join city_data p on p.year = g.year and p.city = 'Paris'
order by g.year;

As you see, there are many ways to write the same query and the DBMS would probably come up with the same execution plan for all.如您所见,编写相同查询的方法有很多种,并且 DBMS 可能会为所有人提供相同的执行计划。 So, just pick the query you like best.因此,只需选择您最喜欢的查询。

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

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