Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用两个表中的多个列进行联接_Mysql_Sql_Join - Fatal编程技术网

Mysql 使用两个表中的多个列进行联接

Mysql 使用两个表中的多个列进行联接,mysql,sql,join,Mysql,Sql,Join,我有两张桌子。第一个表名为price_changes,另一个表名为area。价格变动的结构是: 在这里,列表id、旧价格、新价格是整数,而更改日期是文本 下一个表是area,其结构如下: **listing_id** **built_area**, **used_area** 240509 340 0 230599 0 789 245678 125 175

我有两张桌子。第一个表名为price_changes,另一个表名为area。价格变动的结构是:

在这里,列表id、旧价格、新价格是整数,而更改日期是文本

下一个表是area,其结构如下:

**listing_id** **built_area**, **used_area** 
240509           340             0
230599           0               789
245678           125             175
select sum(new_price) / sum(built_area + used_area)
from (select listing_id, max(new_price) as new_price
      from price_changes
      where old_price < new_price and change_date >= '2016-01-01' and
            change_date < '2017-01-01'
      group by listing_id
     ) l join
     area a
     using (listing_id)
where built_area > 200 or used_area > 200;
这里列出的\u id、build\u区域、used\u区域都是整数值

我想进行但无法进行的Sql查询如下:

计算2016年房价上涨且建筑面积或使用面积>200的物业的平均平方米价格。这里的平均平方米价格意味着将建筑面积和使用面积相加,形成一个称为总面积的新列,对于价格,我必须使用增加的价格列

我尝试用嵌套查询来实现,但没有成功。我提出的嵌套查询是

从fast_course_reg.price changes中选择listing_id,其中new_price>old_price和change_date 比如“2016%”和在 从已建面积>200或已用面积>200的区域中选择列表id


但问题是嵌套查询不允许在嵌套节中添加多个列。对于Join,我不知道该怎么做,因为内部Join不允许从两个表中选择多个列。

要获得2016年价格上涨的属性:

select listing_id, max(new_price)
from price_changes
where old_price < new_price and change_date >= '2016-01-01' and
      change_date < '2017-01-01'
group by listing_id;
然后,您可以将其用作子查询以获取平均值。结果是这样的:

**listing_id** **built_area**, **used_area** 
240509           340             0
230599           0               789
245678           125             175
select sum(new_price) / sum(built_area + used_area)
from (select listing_id, max(new_price) as new_price
      from price_changes
      where old_price < new_price and change_date >= '2016-01-01' and
            change_date < '2017-01-01'
      group by listing_id
     ) l join
     area a
     using (listing_id)
where built_area > 200 or used_area > 200;

编辑您的问题并显示您所做的尝试。