【发布时间】:2013-01-14 22:52:32
【问题描述】:
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=""coulmnfk""
class="table2class"
cascade="none"/>
表2的关系是这样的:
<set name="table1" table=""table1"" inverse="true" cascade="none">
<key column=""coulmnFK""/>
<one-to-many class="table1"/>
</set>
不知道如何包含第一个条件。
【问题讨论】:
-
在您的第一次选择中,您将 where 条件写为“ where t1.columnFK=t1.columnpk” 这是错误的
-
您的更新声明是否也有任何错误?我认为您的更新语句会出错,因为您刚刚写了 somevalue = 'value2'。相反,您应该使用 t2.somevalue = 'value2'。你会试试吗
-
@Himanshu Yadav 这是什么关系? :) 您在父表中有一个子 pk 作为 fk?您不想更新引用父 pk 的子 fk 吗?
-
@AshReva 条件“ where t1.columnFK=t1.columnpk ”是正确的。外键总是引用(或应该)另一个表的主键。有时,其他表中的主键名称作为外键的字段名称,但这只是命名约定,没有对错。
-
如果有帮助,我已经添加了hbm文件中定义的表之间的关系
标签: sql sql-server sql-server-2005