Reputation: 5080
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
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
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.
Upvotes: 1
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