2

I would like to update the values of “area” variable according to the new values of table B

my try:

update a set area=(select area from b)
where a.id=b.id

table A

Id   area
1      N
2      S
3      W
4     W
5     E

table B

Id   area
1      W
2      E
progster
  • 877
  • 3
  • 15
  • 27

2 Answers2

3

This should do it:

update a
  set area = b.area
from a
join b
  on a.id = b.id;
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
1

Try with this

Update a set area= COALESCE((select b.area from b where a.id=b.id),a.area);
  • this overwrite everything in A, I want to update only what it is in B. now I see in A the values of area present in B and the rest is NULL – progster Oct 04 '16 at 14:37
  • Try with Update a set area= COALESCE((select b.area from b where a.id=b.id),a.area); – Navneet Rabadiya Oct 04 '16 at 14:49
  • this works thanks, the only issue is that in real life I have almost 100 variables to update with this criteria of a.id=b.id. could be possible something more flexible? I don't know if it is possible to do this with joins – progster Oct 04 '16 at 14:59
  • Yes, In any case you have to write that. – Navneet Rabadiya Oct 04 '16 at 15:03