我試圖用這個命令用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
,出了什麼問題以及如何糾正呢?
我試圖用這個命令用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
,出了什麼問題以及如何糾正呢?
你忘了你的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);
您需要提及其他部分
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;
試試這個:
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' ;
有中沒有'else'你'if' – ManseUK
你可能需要的其他如果條件的一部分。請參閱http://stackoverflow.com/questions/9916827/mysql-if-statement –