sql - Postgres: update column by id referenced in another table -


i have 2 tables , attempting change value of particular column based on extracting row id table column matches particular value.

table 1 (cc_show) contains column (background_color) , want change value fixed value (ff0000) if table 2 (cc_show_instances) column 'record' or column 'rebroadcast' equal 1.

furthermore, columns in table 2 (cc_show_instances) 'record' or 'rebroadcast' columns not equal 1, change corresponding id in table 1 0000ff.

i can search table 2 (cc_show_instances) positive results following command:

select distinct show_id  cc_show_instances  rebroadcast = 1  or record = 1 

i tried putting select contained in option follows:

update cc_show     set background_color = 'ff0000'  id = (select distinct show_id              cc_show_instances              rebroadcast = 1 or record = 1); 

but returns error:

error: more 1 row returned subquery used expression

which assume because not iterating through results. how can achieve desired results?

you have been told in in comments.
better yet, join second table in from clause. that's bit shorter , in not scale big sets (at least in tests in postgres 8.4 - 9.2).

update cc_show s set    background_color = 'ff0000'   cc_show_instances si  1 in (si.rebroadcast, si.record)  -- in small sets ,    s.id = si.show_id; 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -