我有一个表(称之为tblContractor),如下所示:(第一行故意为空):
ContractorId ContractorName LicenseNumber
0
1 Smith Inc A12345
43 Joe's LLC B4C5t6
4 SureFix Co. 77987
77 ReadyMix 009ABCV
和表tblProject如此:
ProjectId Name ContractorName ContractorId
32 SureFix Co. NULL
40 Joe's LLC NULL
42 ReadyMix NULL
44 Smith Inc NULL
我刚刚创建了最后一列ContractorId,因为我想要规范化数据, 即只有tblContractor中的ContractorName和tblProject中承包商的索引。 所以tblProject看起来像:
ProjectId Name ContractorName ContractorId
32 SureFix Co 77987
40 Joe's LLC B4C5t6
42 ReadyMix 009ABCV
44 Smith Inc A12345
我该怎么做?
这是我到目前为止所做的,但它不起作用:
SELECT project.ProjectId, project.Name, project.ContractorName, project.ContractorId
FROM tblProject project
WHERE NOT EXISTS (
SELECT TOP 1 *
FROM tblContractor contractor
WHERE project.ContractorName = contractor.ContractorName)
Order by ContractorName
对不起表格格式。我查看了有关SO问题的表格格式的问题,我想这确实不是一个很好的方法。
答案 0 :(得分:3)
尝试从此开始,以便移除承包商数据:
update a set a.contractorid=b.contractorid from tblProject a
join tblContractor b on a.name=b.name