2014-09-12 104 views
0

我想將用戶id 1的年份級別從tbl_profile更改爲'3rd',並且我想從表中引用名稱'captain america',因爲他們擁有相同的user_id。加入兩個表並更新列值

這裏是我的示例表:

tbl_profile 
users_id year_level 
1   none 

tbl_usersinfo 
users_id  full_name 
1   captain america 

這裏是我的查詢:

UPDATE tbl_profile AS p 
SET p.year_level = '3rd' 
LEFT JOIN 
tbl_usersinfo AS i 
ON 
i.users_id = p.users_id 
WHERE 
i.full_name = 'captain america'; 

我希望它是這樣的,但我知道,這個查詢是不可能的,因爲我的天堂」 t指定了full_name列的來源:

UPDATE tbl_profile AS p 
SET p.year_level = '3rd' 
WHERE i.full_name = 'captain america' 
LEFT JOIN 
tbl_usersinfo AS i 
ON 
i.users_id = p.users_id 
+1

您可以使用In ...更新...我在哪裏(從tbl_usersinfo選擇user_id,其中full_name ='美國隊長') – okaram 2014-09-12 14:13:40

回答

2

使用此查詢:

UPDATE tbl_profile 
SET year_level='3rd' 
WHERE users_id IN (
    SELECT users_id 
    FROM tbl_usersinfo WHERE full_name = 'captain america'); 
+0

http://sqlfiddle.com/#!2/221a6b/1 – 2014-09-12 14:27:56

+0

WOOOAH,THANKS BROOOOO! :))我真的需要這個查詢。 – 2014-09-12 14:29:19

+0

@monkeycoder檢查將幫助你http://sqlfiddle.com/#!2/221a6b/1 – 2014-09-12 14:30:33