1

I have 2 tables, table_a and table_b. I want to get user_id from table_a and update a record in table_b for that user_id. Something like this:

Select * from table_a where user_id ="Ken"

if result is 22 

Update score = 1 where id = 22. 

These queries work but I want to combine them in one. Can anyone please tell me what is the best way to do this?

1
  • 1
    What version of SQL are you using? mysql/oracle/sql server? These different RDBMS will have different options. Jun 24, 2012 at 5:34

3 Answers 3

6

Try this this should work,

UPDATE table_b
SET score=1
WHERE id = (select id from table_a where user_id ="Ken")
0
3
UPDATE table_b SET score = 1
WHERE id IN (SELECT * FROM table_a WHERE user_id = "Ken")
1
  • 1
    Ah good catch pst. Might help if I reference the table being updated :) Jun 24, 2012 at 5:46
1
IF EXISTS (Select * from table_a where user_id ="Ken" )
If result=22
BEGIN
UPDATE table_a SET score = 1
END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.