0

I am trying to use this command to update saving.balance using IF statement:

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;

However, MySQL gives me ERROR 1064, what's wrong and how to correct it?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Choi Shun Chi
  • 93
  • 3
  • 7

3 Answers3

2

you forgot the 3th argument in your the IF function and other syntax stuff :-)

why you make you script not with where? like this:

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';

you will update just records with 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);
silly
  • 7,789
  • 2
  • 24
  • 37
2

Try this instead:

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';

Or:

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' ;
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
1

You need to mention the else part

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;
senK
  • 2,782
  • 1
  • 27
  • 38