如何引用或连接具有varchar主键的表和具有INT主键的表(SQL服务器)

时间:2018-04-14 18:00:31

标签: sql sql-server compiler-errors

这是我正在使用的数据库。

Database

我想要做的是将department表引用或加入项目表。 我应该能够成功使用以下查询。我需要这个查询特定的结果

SELECT P.ProjectID, P.ProjectName, P.Department, D.BudgetCode
FROM DEPARTMENT D, PROJECT P

此查询成功执行。但是,我在结果中得到重复的值,并且格式不正确。这是我得到的结果

ProjectID   ProjectName                     Department      BudgetCode
1000        2010 Q3 Product Plan            Marketing       BC-300-10                     
1000        2010 Q3 Product Plan            Marketing       BC-100-10                     
1000        2010 Q3 Product Plan            Marketing       BC-400-10                     
1000        2010 Q3 Product Plan            Marketing       BC-500-10                     
1000        2010 Q3 Product Plan            Marketing       BC-800-10                     
1000        2010 Q3 Product Plan            Marketing       BC-200-10                     
1000        2010 Q3 Product Plan            Marketing       BC-700-10                     
1000        2010 Q3 Product Plan            Marketing       BC-600-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-300-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-100-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-400-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-500-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-800-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-200-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-700-10                     
1100        2010 Q3 Portfolio Analysis      Finance         BC-600-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-300-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-100-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-400-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-500-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-800-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-200-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-700-10                     
1200        2010 Q3 Tax Preparation         Accounting      BC-600-10                     
1300        2010 Q4 Product Plan            Marketing       BC-300-10                     
1300        2010 Q4 Product Plan            Marketing       BC-100-10                     
1300        2010 Q4 Product Plan            Marketing       BC-400-10                     
1300        2010 Q4 Product Plan            Marketing       BC-500-10                     
1300        2010 Q4 Product Plan            Marketing       BC-800-10                     
1300        2010 Q4 Product Plan            Marketing       BC-200-10                     
1300        2010 Q4 Product Plan            Marketing       BC-700-10                     
1300        2010 Q4 Product Plan            Marketing       BC-600-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-300-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-100-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-400-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-500-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-800-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-200-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-700-10                     
1400        2010 Q4 Portfolio Analysis      Finance         BC-600-10   

我需要获得此结果

enter image description here

我尝试使用JOIN:

SELECT P.ProjectID, P.ProjectName, P.Department, D.BudgetCode
FROM DEPARTMENT D
RIGHT JOIN PROJECT P ON P.ProjectID = D.DepartmentName

但是我收到转换错误

  

将varchar值'Accounting'转换为数据类型int时转换失败。

我假设是因为无法将varchar转换为int。除非演员可以工作......

SELECT P.ProjectID, P.ProjectName, P.Department, D.BudgetCode
FROM DEPARTMENT D
RIGHT JOIN PROJECT P ON cast(P.ProjectID AS varchar) = D.DepartmentName

使用演员表,我得到了这个结果

ProjectID   ProjectName                     Department      BudgetCode
1000        2010 Q3 Product Plan            Marketing       NULL
1100        2010 Q3 Portfolio Analysis      Finance         NULL
1200        2010 Q3 Tax Preparation         Accounting      NULL
1300        2010 Q4 Product Plan            Marketing       NULL
1400        2010 Q4 Portfolio Analysis      Finance         NULL

这几乎就是我所寻找的,但它显示了我的BudgetCodes的NULL值,这是不正确的。它们不是空值。

所以在这一点上,我没有想法。 (作为旁注,我理解在我的数据库中我有一个varchar作为主键,我试图用一个int的主键来加入那个主键。如果它们属于同一类型,我确定我的加入会有效)

1 个答案:

答案 0 :(得分:1)

如果Project.Department是Department.DepartmentName的forigen键,那么你可以加入。

JOIN Project P ON P.Department = D.DepartmentName

如果没有,您需要查看两个表Project和Department之间共享哪个列,并按该列链接两个表。共享列需要在两个表上具有相同的数据类型。

另一个提示是,您正在查询Department和JOIN Projects,同时,您只使用Department的BudgetCode,其余来自Project。在这种情况下,您需要查询Project和JOIN Department。

SELECT 
    P.ProjectID, 
    P.ProjectName, 
    P.Department, 
    D.BudgetCode
FROM PROJECT P
INNER JOIN DEPARTMENT D ON D.DepartmentName = P.Department