gpt4 book ai didi

mysql - 连接两个表中的多个列

转载 作者:行者123 更新时间:2023-11-29 10:39:32 25 4
gpt4 key购买 nike

我有两张 table 。第一个表名为 price_changes,另一个表名为 areaprice_changes的结构是:

**listing_id**  **old_price** **new_price** **change_date**
240509 180999 100234 2016-03-30
230599 165789 189760 2017-06-12
245678 123456 176432 2016-12-08

这里的listing_idold_pricenew_price是整数,而change_date是文本。

下一个表是区域,其结构如下:

**listing_id** **built_area**, **used_area** 
240509 340 0
230599 0 789
245678 125 175

这里的listing_idbuilt_areaused_area都是整数值。

我想要执行但无法执行的 Sql 查询是这样的:

计算 2016 年建成或使用面积 > 200 且价格上涨的特性的平均平方米价格。这里的平均平方米价格意味着将建成面积已使用面积相加 形成一个名为 total_area 的新列,对于价格,我必须使用 increased_price 列。

我尝试使用嵌套查询来做到这一点,但没有成功。我提出的嵌套查询是

从 fast_course_reg.价格更改中选择listing_id,其中new_price > old_price 和change_date比如“2016%”和listing_id(从built_area > 200或used_area > 200的区域中选择listing_id);

但问题是嵌套查询不允许在嵌套部分添加多于一列。对于联接,我不知道如何做到这一点,因为内部联接不允许从两个表中选择多个列。

最佳答案

要获取 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;

然后您可以将其用作子查询来获取平均值。结果是这样的:

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;

关于mysql - 连接两个表中的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45772841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com