foreign keys - SQL Query - If discriminator in table equals something, change column content in another table to equal PK -
say have 2 tables(tablea , tableb)
tablea has id (primary key) , discriminator value (eg. "stuff") tableb has id column foreign key of tablea id ("stuffid")
what make sql query tableb checks discriminator value "stuff" (for example) in tablea , if value "stuff", take id (primary key) of tablea , link value tableb in column "stuffid"
is doable?
thank you
yes there existing rows.
tablea might have
id discriminator -------------------- 1 stuff 2 notstuff 3 stuff
tableb might have
stuffid name ----------------- 43 ffr 87 fd
i stuffid column same column in tablea if discriminator in tablea "stuff"
ok, reposting comment answer:
insert tableb (stuffid) select id tablea discriminator='stuff'
should accomplish want stated in original post. note new rows added in tableb have null or default value in name field isn't specified in query.
edit: depending on design of tableb regards uniqueness , constraints query might not give correct result (for instance might fail if there rows stuffid present in tableb if they're supposed unique, or might add duplicate rows if they're not unique). without detailed description of tables , how related it's difficult provide answer.