2012-12-12 54 views
0

我试图用这个命令用IF语句来更新saving.balance使用if语句更新表

UPDATE TABLE saving s, time t 
SET s.balance = IF(t.currency_TYPE = ‘RMB’, s.balance + t.balance * t.interest) 
WHERE t.ID = 'input' 
AND s.User = t.User; 

然而,MySQL的给我ERROR 1064,出了什么问题以及如何纠正呢?

+0

有中没有'else'你'if' – ManseUK

+0

你可能需要的其他如果条件的一部分。请参阅http://stackoverflow.com/questions/9916827/mysql-if-statement –

回答

2

你忘了你的IF函数和其他语法的东西:-)

这是第3争论为什么你让你的脚本不是在哪里?像这样:

UPDATE saving s 
INNER JOIN time t 
    ON t.ID = 'input' 
    AND t.User = s.User 
SET s.balance = s.balance + t.balance * t.interest 
WHERE t.currency_TYPE = 'RMB'; 

您将更新currency_type rmb的记录!

OR

UPDATE saving s 
INNER JOIN time t 
    ON t.ID = 'input' 
    AND t.User = s.User 
SET s.balance = (t.currency_TYPE = 'RMB', s.balance + t.balance * t.interest, 0); 
1

您需要提及其他部分

UPDATE TABLE saving s, time t 
SET s.balance = IF(t.currency_TYPE = ‘RMB’, s.balance + t.balance * t.interest , 0) 
WHERE t.ID = 'input' 
AND s.User = t.User; 
2

试试这个:

UPDATE saving s 
INNER JOIN `time` t ON s.`User` = t.`User` 
SET s.balance = CASE 
        WHEN t.currency_TYPE = 'RMB' THEN s.balance + 
                t.balance * t.interest 
        ELSE s.balance -- Don't forgot this, default is NULL 
       END 
WHERE t.ID = 'input'; 

或者:

UPDATE saving s 
INNER JOIN `time` t ON s.`User` = t.`User` 
SET s.balance = s.balance + t.balance * t.interest 
WHERE t.ID = 'input' 
    AND t.currency_TYPE = 'RMB' ;