简体   繁体   中英

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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