2015-04-15 54 views
0

我有一张表,其中我插入价值的平均值在另一个表中。 我正在使用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查询。

回答

0

您可以使用:

ON DUPLICATE KEY UPDATE `rental_figure` = VALUES(`rental_figure`) 

VALUES()返回假若没有重复的插入值。

+0

感谢Vatev为您的工作提供帮助。 –