Tuesday 25 February 2014

Update table with join

Update  table with join.

We have two table with name s1 and s2
In first table s1 we have add1 column with null value.
We have another table s2 that contains loc column . Now we need a sql query to update table s1 add1 column with second s2 loc column.

Table name s1

id      Name                add1
1      Rahul                NULL
2      Ramu                NULL
3      Sachi                NULL
4      Sakshi              NULL
5      Aki                   NULL

Table name s2

Id     loc
1      Mahipalpur
2      Bettiah
3      Banglore
4      Gurgaon
5      Noida

The Sql Query for update with join is given bellow:-
update s1 set add1=st2.loc from s1 st1 join s2 st2 on st1.id=st2.id
id              Name                        add1
1              Rahul                        Mahipalpur
2              Ramu                        Bettiah
3              Sachi                        Banglore
4              Sakshi                      Gurgaon
5              Aki                           Noida


0 comments: