连接两个表并更新列值

时间:2014-09-12 13:56:47

标签: mysql sql

我想将用户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';

我希望它是这样的,但我知道这个查询是不可能的,因为我还没有指定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 个答案:

答案 0 :(得分:2)

使用此查询:

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