0

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

SELECT bed, bath, type, suburb, postcode, AVG(price)
FROM rent_suburbs
GROUP BY bed, bath, type, suburb, postcode

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

我正在尝试修复低于某人建议的代码,但我被卡住了。

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 ????

我是新来的,所以任何帮助表示赞赏。谢谢

表结构如下:

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)

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)
4

1 回答 1

1

试试这个:

   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;
于 2013-11-05T13:24:40.357 回答