使用if条件从另一个表中添加一个带有值的列

时间:2015-12-07 02:56:58

标签: mysql

我有两张表就是这样......

table2015                                table2016
=======+===========+========             =======+===========+========
id     |remains    |days_off             id     |remains    |days_off 
=======+===========+========             =======+===========+========   
1      |4          |12                    1     |12         |12
2      |-2         |12                    2     |12         |12
3      |3          |25                    3     |25         |25

如果table2015上的遗留物小于0,我怎么能在table2016上编辑days_off?这是需要的结果:

table2015                                table2016
id     |remains    |days_off             id     |remains    |days_off 
=======+===========+========             =======+===========+========   
1      |4          |12                    1     |12         |12
2      |-2         |12                    2     |12         |10
3      |0          |25                    3     |25         |25

我已经尝试过此查询,但它在第5行给了我一个错误

UPDATE 
    table2016
SET 
    table2016.days_off = table2016.days_off + table2015.remains
FROM 
    table2016 
JOIN 
    table2015
ON 
    table2016.id = table2015.id
AND 
    table2015.remains < 0

0 个答案:

没有答案