join - Update a table's field using another table's field to map it with third table in Postgresql -


enter image description here

i have 3 tables : landmark, trans , output. schema test schema. green tables,blue highlighted columns. mapping can done between country , region.
have home_country having home_region null right now.i need find value home_region home_country has home region null.

the table should updated
if landmark.pgs_country = output.home_country set landmark.region = output.home_region.

but if finds no match country us-gm in output table landmark - country_name should map trans table check

if output.home_country = trans.ib_country, take trans.xperia_country match , equate landmark.country_name ie trans.xperia_country = landmark.country_name , set landmark.region = output.home_region find home_region country us-gm. update must set landmark.region = output.home_region.

how update output table?

like this:

with possible_joins ( select     o.home_country     , coalesce(l1.region, l2.region) region     output o     left join landmark l1 on l1.pgs_country = o.home_country     left join trans t on t.ib_country = o.home_country     left join landmark l2 on l2.country_name = t.xperia_country     o.home_region null )  update     output o set     home_region = p.region     possible_joins p     o.home_country = p.home_country     , o.home_region null; 

this done in easier way, if define more rules in model, meaning impose better integrity. i'm not saying 'you have to', suppose can't (or maybe hole thing working on in first place). want explain, use of left joins, seems can't take granted.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -