SQL Update使用另一个表中的“where”值?

时间:2012-06-24 05:30:51

标签: sql

我有2个表,table_atable_b。我想从user_id获取table_a并更新table_buser_id的记录。像这样:

Select * from table_a where user_id ="Ken"

if result is 22 

Update score = 1 where id = 22. 

这些查询有效但我希望将它们合二为一。谁能告诉我这样做的最佳方法是什么?

3 个答案:

答案 0 :(得分:6)

试试这个应该可行,

UPDATE table_b
SET score=1
WHERE id = (select id from table_a where user_id ="Ken")

答案 1 :(得分:3)

UPDATE table_b SET score = 1
WHERE id IN (SELECT * FROM table_a WHERE user_id = "Ken")

答案 2 :(得分:1)

IF EXISTS (Select * from table_a where user_id ="Ken" )
If result=22
BEGIN
UPDATE table_a SET score = 1
END