子查询每年的价值变化

时间:2013-04-27 22:53:26

标签: mysql subquery mysql-error-1093

此查询出现1093错误:

UPDATE values_table as outer_select 
    SET annual_change = sample_value - (
        SELECT sample_value FROM values_table 
        WHERE date_sampled = DATE_SUB(outer_select.date_sampled, INTERVAL 1 YEAR)
    );

我正在尝试为每一行设置annual_change,等于当前行sample_value减去去年的sample_value

数据不会回到开始时间,那么如何处理缺乏历史价值呢?

1 个答案:

答案 0 :(得分:1)

试试这个

UPDATE values_table as a
join  values_table as b 
ON b.date_sampled = DATE_SUB(a.date_sampled, INTERVAL 1 YEAR)
set a.annual_change = a.sample_value - b.sample_value