1

这个查询

SELECT P2.ID, P2.Storenum, [Periods_2013-2014].[PeriodDate] AS [OpenFromDate],
  P2.[Open From Day], P2.[Open From Time], P2.[Open To Day], 
  P2.[Open To Time]
FROM [Periods_2013-2014] 
INNER JOIN [tblOpenCloseTimes] AS P2 ON [Periods_2013-2014].[Day]=P2.[Open From Day]
WHERE ((([Periods_2013-2014].Day)=P2.[Open From Day] 
  AND P2.Storenum ="43010605" 
  AND [Periods_2013-2014].Period = "4"))

导致“输入参数值”Query1.PeriodDate作为请求的参数,而如果我只是AS [OpenFromDate]从第一行中删除“”,则查询运行完美。[Periods_2013-2014]如果我将表别名为 P1 ,它的作用相同。PeriodDate除了我需要堆叠查询以获取该字段两次之外,这不会是一个问题。我究竟做错了什么?


更新:这个查询

SELECT T2.Storenum, T2.[PeriodDate] AS [Open From Date], T2.[Open From Day], 
T2.[Open From Time], T1.[PeriodDate] AS [Open To Date], T2.[Open To Day], 
T2.[Open To Time]
FROM [Periods_2013-2014] AS T1, 
(SELECT P2.Storenum, P1.[PeriodDate],  P2.[Open From Day], P2.[Open From Time], 
P2.[Open To Day], P2.[Open To Time]
FROM [Periods_2013-2014] AS P1 INNER JOIN tblOpenCloseTimes AS P2 
ON P1.[Day]=P2.[Open From Day]
WHERE (((P1.Day)=P2.[Open From Day] 
   AND P2.Storenum ="43010605" 
   AND P1.Period = "4"))
ORDER BY P1.[PeriodDate]) AS T2
WHERE T1.[Day]=T2.[Open To Day]
AND T1.[PeriodDate]>=T2.[PeriodDate] 
AND T1.[PeriodDate] - T2.[PeriodDate] <7
ORDER BY P1.[PeriodDate];

现在我已经从属性表中删除了 Order By 子句,现在工作得很好。

4

1 回答 1

2

Access 不允许您在查询中使用字段别名。所以,我想问题出在你的Where条款中。

尝试删除 name: P2并使用完整的表名,以便您的语句如下所示:

SELECT [tblOpenCloseTimes].ID, [tblOpenCloseTimes].Storenum, [Periods_2013-2014].[PeriodDate] AS [OpenFromDate],
  [tblOpenCloseTimes].[Open From Day], [tblOpenCloseTimes].[Open From Time], [tblOpenCloseTimes].[Open To Day], 
  [tblOpenCloseTimes].[Open To Time]
FROM [Periods_2013-2014] 
INNER JOIN [tblOpenCloseTimes] ON [Periods_2013-2014].[Day]=[tblOpenCloseTimes].[Open From Day]
WHERE ((([Periods_2013-2014].Day)=[tblOpenCloseTimes].[Open From Day] 
  AND [tblOpenCloseTimes].Storenum ="43010605" 
  AND [Periods_2013-2014].Period = "4"))

我想知道为什么您对其中一个表使用别名而不是其他表。对列名使用别名很好。但是,不应在 Where 子句中使用这些别名。

此外,您可以参考以下链接了解更多详情:

于 2013-07-28T12:10:55.897 回答