我有一个表,其中插入的值是另一个表中价格的平均值。 我正在使用 ON DUPLICATE KEY UPDATE 查询,但问题是我正在更新子查询中的值,并且它返回列不能为空的错误。
insert into `averge_figures` (`full_postcode`,`property_type`,`bedrooms`,`rental_figure`)
select p.full_postcode,p.property_type,p.bedrooms,
((select avg(p2.price) as price1 from property p2 where p.full_postcode=p2.full_postcode and p.bedrooms=p2.bedrooms
and p.property_type=p2.property_type and p2.trans_type_id=2 )+
(select avg(s.price) as price2 from sale_detail s where s.proptype=p.property_type and s.bedrooms=p.bedrooms
and s.postcode=p.full_postcode and s.rentorsale='R'))/2
from property p
ON DUPLICATE KEY UPDATE `rental_figure`=
((select avg(p2.price) as price1 from property p2 where p.full_postcode=p2.full_postcode and p.bedrooms=p2.bedrooms
and p.property_type=p2.property_type and p2.trans_type_id=2 )+
(select avg(s.price) as price2 from sale_detail s where s.proptype=p.property_type and s.bedrooms=p.bedrooms
and s.postcode=p.full_postcode and s.rentorsale='R'))/2
任何人都可以纠正我的 mysql 查询吗?
最佳答案
您可以使用:
ON DUPLICATE KEY UPDATE `rental_figure` = VALUES(`rental_figure`)
VALUES()
返回如果没有重复项则将插入的值。
关于mysql - 带子查询的 ON DUPLICATE KEY UPDATE 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647768/