这个多内连接查询有什么问题?

时间:2012-12-12 13:39:35

标签: sql ms-access join recursion

我正在尝试执行下面的SQL查询(在Access数据库中),我收到类型不匹配错误。

SELECT S1.[Main Project] AS Company, S1.[Main Link] AS Project, S1.[Name] AS Milestone, S1.[Start] As MilestoneDueDate, 
S2.[Name] As Successor1, S2.[Start] as SuccessorDueDate1, 
S3.[Name] As Successor2, S2.[Start] as SuccessorDueDate2, 
S4.[Name] As Successor3, S2.[Start] as SuccessorDueDate3, 
S5.[Name] As Successor4, S2.[Start] as SuccessorDueDate4, 
S6.[Name] As Successor5, S2.[Start] as SuccessorDueDate5, 
S7.[Name] As Successor6, S2.[Start] as SuccessorDueDate6, 
S8.[Name] As Successor7, S2.[Start] as SuccessorDueDate7, 
S9.[Name] As Successor8, S2.[Start] as SuccessorDueDate8, 
S10.[Name] As Successor9, S2.[Start] as SuccessorDueDate9, 
S11.[Name] As Successor10, S2.[Start] as SuccessorDueDate10

FROM ((((((((((Sheet1 AS S1 
INNER JOIN Sheet1 AS S2 
ON S1.[Outcoming Task 1] = S2.[ID])
INNER JOIN Sheet1 AS S3 
ON S1.[Outcoming Task 2] = S3.[ID])
INNER JOIN Sheet1 AS S4 
ON S1.[Outcoming Task 3] = S4.[ID])
INNER JOIN Sheet1 AS S5 
ON S1.[Outcoming Task 4] = S5.[ID])
INNER JOIN Sheet1 AS S6 
ON S1.[Outcoming Task 5] = S6.[ID])
INNER JOIN Sheet1 AS S7 
ON S1.[Outcoming Task 6] = S7.[ID])
INNER JOIN Sheet1 AS S8 
ON S1.[Outcoming Task 7] = S8.[ID])
INNER JOIN Sheet1 AS S9 
ON S1.[Outcoming Task 8] = S9.[ID])
INNER JOIN Sheet1 AS S10 
ON S1.[Outcoming Task 9] = S10.[ID])
INNER JOIN Sheet1 AS S11
ON S1.[Outcoming Task 10] = S11.[ID])

WHERE S1.Start >S2.[Start]
OR S1.Start >S3.[Start]
OR S1.Start >S4.[Start]
OR S1.Start >S5.[Start]
OR S1.Start >S6.[Start]
OR S1.Start >S7.[Start]
OR S1.Start >S8.[Start]
OR S1.Start >S9.[Start]
OR S1.Start >S10.[Start]
OR S1.Start >S11.[Start]

ORDER BY S1.[Main Project] 

我是否正确使用内连接?

我知道这张桌子太可怕了(实际上开发它的人应该创建一个关系表),但它是:(

有更优雅的方法来解决它吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

类型不匹配表明Access正在尝试比较两种不同类型的东西。这可能是导致这种情况的20个比较语句中的一个或多个。我建议您尝试使用简单查询重现此错误,例如:

select 1 from Sheet1 as S1 inner join Sheet1 as S2
   on S1.[Outcoming Task 1] = S2.[ID];

以这种方式测试不同的字段,直到您看到类型不匹配的位置。

就更优雅的解决方案而言,这取决于您要做的事情。如果您使用此查询的目标说明更新问题,我们可以帮助您找到更好的解决方案。

如果您必须对此数据进行大量工作,我建议您尽快将其从错误格式中删除。如果您可以重新构建源文件,那很好。如果没有,仍然可能值得制作一个以逻辑方式格式化的临时表,并使用它们来完成你需要做的任何工作。