2015-04-15 90 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爲您的工作提供幫助。 –