简体   繁体   English

连接两个表并更新列值

[英]joining two tables and update a column value

I want to change the value of the year level of users id 1 from tbl_profile to '3rd', and i want to reference the name 'captain america' from the table because they have the same user_id. 我想将用户id 1的年级别的值从tbl_profile更改为“ 3rd”,并且我想从表中引用名称“ captain America”,因为他们具有相同的user_id。

here is my example table: 这是我的示例表:

tbl_profile
users_id    year_level
1           none

tbl_usersinfo
users_id        full_name
1           captain america

here is my query: 这是我的查询:

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';

I want it to be like this, but i know that this query is not possible because i haven't specified where the full_name column came from: 我希望它像这样,但是我知道不可能执行此查询,因为我没有指定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

Use this query: 使用此查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM