Need help on a update sql with a join state
Here is the example. Here I am taking up two tables xt1 and xt2. Both have
primary key "id". The purpose is to update the name field of xt1 if the
grade field of xt2 = 'a' and xt1.id = xt2.id.
create table xt1
(
id int,
name varchar(10)
)
insert into xt1 values (1, 'abc')
insert into xt1 values (2, 'xyz')
insert into xt1 values (3, 'aaa')
insert into xt1 values (4, 'bbb')
insert into xt1 values (5, 'ccc')
insert into xt1 values (6, 'xdz')
create table xt2
(
id int,
grade varchar(10)
)
insert into xt2 values (1, 'b')
insert into xt2 values (2, 'b')
insert into xt2 values (3, 'c')
insert into xt2 values (4, 'a')
insert into xt2 values (5, 'a')
insert into xt2 values (6, 'a')
select * from xt1
select * from xt2
update xt1
set xt1.name = xt1.name + ' I am changed'
from xt1 inner join xt2
on xt1.id = xt2.id
where xt2.grade = 'a'
|