首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MySQL项目设计-基于另一个select查询的行从一个表中有条件地进行选择

MySQL项目设计-基于另一个select查询的行从一个表中有条件地进行选择
EN

Stack Overflow用户
提问于 2013-11-05 12:12:33
回答 1查看 189关注 0票数 0

我在一个MySQL数据库中有两个表,定期添加新数据。一份是出租物业清单及其特点,如2张床、2间浴室、地点、租金等。第二张表是目前待售的物业清单。从租房表中,我可以使用查询确定特定类型的财产在特定位置的平均租金:

代码语言:javascript
运行
复制
SELECT bed, bath, type, suburb, postcode, AVG(price)
FROM rent_suburbs
GROUP BY bed, bath, type, suburb, postcode

我希望能够从buy_items表中选择属性,其中用户定义的销售价格百分比低于匹配类型和位置的属性的平均租赁价格。

我试图修复下面的代码,有人建议,但我被困住了。

代码语言:javascript
运行
复制
select listing, bed, bath, type, address, postcode, state, price
from 
   buy_items
where (price*.0014) < avg(price) from 
select 
    bed, bath, type, suburb, postcode, avg(price)
from
    rent_items
group by bed , bath , type , suburb , postcode
/* and bed, bath, type, suburb and postcode match the buy_items property ????

我是新来的,所以任何帮助都很感激。谢谢

表的结构如下:

代码语言:javascript
运行
复制
buy_items

buy_items_id2   int(11)
car         int(11)
price   int(11)
listing varchar(12)
bed         int(11)
bath    int(11)
suburb  varchar(25)
state   int(11)
scrapedate  int(11)
address varchar(45)
type    varchar(25)
postcode    int(11)

代码语言:javascript
运行
复制
rent_items

rent_items_id2  int(11)
car         int(11)
price   int(11)
listing int(11)
bed         int(11)
bath    int(11)
suburb  varchar(25)
state   varchar(5)
scrapedate  int(11)
address varchar(45)
type    varchar(25)
postcode    int(11)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-05 13:24:40

试试这个:

代码语言:javascript
运行
复制
   select listing, b.bed, bath, type, address, postcode, state, price
    from 
       buy_items b
    join (
    SELECT bed, bath, type, suburb, postcode, AVG(price) as avg_price
    FROM rent_suburbs
    GROUP BY bed, bath, type, suburb, postcode ) a
    on a.bed=b.bed and a.bath=b.bath and a.suburb=b.suburb and a.postcode=b.postcode
    where (b.price*.0014) < a.avg_price;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19788717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档