我有以下表格结构:
Table1: BaseObject (ID, Name)
Table2: Instance (BaseObjectID, Type)
Table3: Mapping (Type, Value)
我想
Update the BaseObject.Name = Mapping.Value
where exists an Instance for the
BaseObject (Instance.BaseObjectID = BaseObject.ID)
and Mapping.Type = Instance.Type
无法弄清楚如何在SQL中实现这一目标。
编辑: 以下工作,但我不确定它是否是最佳的:
UPDATE BaseObject
SET BaseObject.Name = (SELECT Mapping.Value
FROM Mapping, Instance
WHERE Mapping.Type = Instance.Type
AND Instance.BaseObjectId = BaseObject.ID)
WHERE EXISTS (SELECT Instance.BaseObjectID
FROM Instance
WHERE Instance.BaseObjectID = BaseObject.ID)
答案 0 :(得分:0)
UPDATE (SELECT t1.Name name,
t3.value value
FROM BaseObject t1,
Instance t2,
Mapping t3
WHERE t2.BaseObjectId = t1.ID
AND t3.Type = t2.Type)
SET name = value
答案 1 :(得分:0)
您可以执行以下操作。请注意,仅当每个Instance
最多只有一个BaseObject
时,它才有效。否则,您必须根据选择适当的名称来定义逻辑。
update BaseObject b
set name = (
select m.value
from Instance i join Mapping m on (i.Type = m.Type)
where BaseObjectID = b.ID
)
where exists (
select 1
from Instance i join Mapping m on (i.Type = m.Type)
where BaseObjectID = b.ID
)