如果我有这样的表:
Materials
(MaterialID,名称)Teacups
(TeacupID,描述,FriendlyName,MaterialID)带有如下数据:
材料:
(1, 'Glass')
(2, 'Porcelain')
(3, 'Iron')
茶杯:
(1, 'Desc1', 'Name1', 1)
(2, 'Desc2', 'Name2', 1)
(3, 'Desc3', 'Name3', 1)
(4, NULL, 'Name1', 2)
(5, NULL, 'Name2', 2)
(6, 'Desc6', 'Name3', 2)
(7, 'Desc7', 'Name1', 3)
(8, NULL, 'Name2', 3)
(9, NULL, 'Name3', 3)
如何更新所有描述(无论是否为NULL),使其具有与的茶杯相同的描述MaterialID=1
,并以FriendlyName匹配
因此数据需要看起来像:
茶杯:
(1, 'Desc1', 'Name1', 1)
(2, 'Desc2', 'Name2', 1)
(3, 'Desc3', 'Name3', 1)
(4, 'Desc1', 'Name1', 2)
(5, 'Desc2', 'Name2', 2)
(6, 'Desc3', 'Name3', 2)
(7, 'Desc1', 'Name1', 3)
(8, 'Desc2', 'Name2', 3)
(9, 'Desc3', 'Name3', 3)
最简单的方法是JOIN clause
在UPDATE
语句中使用。试试这个..
UPDATE A
SET A.Description = B.Description
FROM Teacups A
JOIN (SELECT Description,
FriendlyName
FROM teacups
WHERE MaterialID = 1) b
ON a.FriendlyName = B.FriendlyName
Where A.MaterialID != 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句