Ondrej Tokar
Ondrej Tokar

Reputation: 5080

Error in inner join update sql

I have 2 tables. In one table I have id and in another I have userId. This is the only connection between them.

Problem is: In one table I have username and in another I have his balance. I want to get user's balance according to his username. Since there is only one column which is connecting them and it is ID, I need to find a way how to update users balance if I have only his username.

Here is what I have tried but it is not working:

  UPDATE t1
  SET t1.balance = '999'
  FROM bitcoin.accountbalance AS t1
  INNER JOIN bitcoin.webusers AS t2
  ON t1.userId = t2.id
  WHERE t2.username = 'simpanz';

EDIT:

ERROR IS: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM bitcoin.accountbalance AS t1   INNER JOIN bitcoin.webusers AS t2     WHERE ' at line 3

I use MySQL.

Upvotes: 1

Views: 93

Answers (3)

iSaumya
iSaumya

Reputation: 1623

If you are using MySQL try this and let me know if it works:

UPDATE bitcoin.accountbalance t1
INNER JOIN bitcoin.webusers t2
ON t1.userId = t2.id
SET t1.balance = '999'
WHERE t2.username = 'simpanz';

Upvotes: 1

echo_Me
echo_Me

Reputation: 37243

Try this

 UPDATE  bitcoin.accountbalance AS t1

 INNER JOIN bitcoin.webusers AS t2
 ON t1.userId = t2.id
 SET t1.balance = '999'
 WHERE t2.username = 'simpanz';

you have to join tables and then SET what you want.

check this

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271013

This will work in both SQL Server and MySQL:

UPDATE accountbalance
  SET balance = '999'
  WHERE EXISTS (SELECT 1
                FROM webusers
                WHERE webusers.id = accountbalance.userId AND
                      webusers.username = 'simpanz'
               );

Upvotes: 1

Related Questions