簡體   English   中英

根據Where條件,用子表的PK更新父表的FK列

[英]Update FK column of the parent table with the PK of child table based on Where condition

select t1.columnFK from table1 t1, table2 t2 where t1.columnFK=t1.columnpk AND t2.somecolumn='value1'



select t2.columnPK from table2 t2 where t2.somecolumn='value2'

所以我必須用第二個選擇語句的值更新第一個選擇語句的所有值。 我試着這樣編寫更新查詢:

UPDATE table1
SET table1.columnFK = table2.columnPK
From tabel1 t1, table2 t2
Where t1.columnfk=t2.columnpk AND somevalue='value2'

這是table2.columnpk的關系,稱為table1.coulmnfk。
table1.hbm.xml

<many-to-one
            name="table2"
            column="&quot;coulmnfk&quot;"
            class="table2class"
            cascade="none"/>

表2關系如下所示:

<set name="table1" table="&quot;table1&quot;" inverse="true"  cascade="none">
                <key column="&quot;coulmnFK&quot;"/>
                <one-to-many class="table1"/>
            </set>

不知道我該如何包括第一個條件。

我仍然覺得您的參照完整性有些奇怪,無法接受。 通常,子表fk用父pk更新。

以下是你可能需要使用的邏輯,假設你有cureently PK家長在孩子talble一個FK和您要添加子PK的FK到父表...使用join

UPDATE parent
SET parent.childPK = child.PK    
FROM
    Parent
    JOIN
    child ON parent.PK = child.ParentPK
WHERE parent.somecolumn in ('value1','value2')
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM