使用自连接时 MS Access 2010 中没有当前记录错误
我正在用自联接替换子查询,以提高查询的性能。
旧的子查询是这样的:
(SELECT fage2.agecat
FROM people AS fage2
WHERE fage2.aacode = people.aacode
AND fage2.persno = 2) AS RAge2,
新的自连接是这样的:
(SELECT [People].[AgeCat]
FROM [People]
INNER JOIN [People] AS p2
ON [People].[aacode] = [P2].[aacode]
WHERE [P2].[PERSNO] = 2 ) AS RAge2,
但返回“无当前记录”错误消息。
目标是找到具有相同 aacode 但 PERSNO 编号为 2 的记录,并在名为 RAge2 的列中返回该记录的 AgeCat,
这只是完整解释的较大查询的一部分 转换查看同一个表中的另一条记录时将 SQL 子查询连接到 Access 2010
I'm replacing a subquery with an self join to improve performance of my query.
The old subquery was like this:
(SELECT fage2.agecat
FROM people AS fage2
WHERE fage2.aacode = people.aacode
AND fage2.persno = 2) AS RAge2,
The new self join is like this:
(SELECT [People].[AgeCat]
FROM [People]
INNER JOIN [People] AS p2
ON [People].[aacode] = [P2].[aacode]
WHERE [P2].[PERSNO] = 2 ) AS RAge2,
but returns a No Current Record error message.
The goal is to find the record that has the same aacode but has the PERSNO number of 2 and return the AgeCat for that record in a column called RAge2,
This is only part of a larger query which is explained in full Convert a SQL subquery into a join when looking at another record in the same table Access 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,看起来您想要优化的这个查询是一个更大查询的一部分,并且对于您发布整个查询的问题很重要,因此它将有助于理解您的问题......
另外,从我所看到的将显示具有相同 AACode 的两行的 RAge2,而不仅仅是像您在目标中所说的那样显示 Persno = 2 的行。粘贴整个查询也将有助于理解这一点。
我试图理解您的查询,因此我为您的原始查询创建了一个假查询:
要获得相同的结果,您需要左连接而不是内连接,因为带有子查询的查询不会' t 从外表中排除结果,因此您将得到类似这样的结果 Join 查询:
Huum, looks like this query that you want to optimize is part of a bigger query, and would be important to the question that you post the entire query so it would help on understanding your problem ...
Also, from what I can see you would be showing the RAge2 for both rows with same AACode not only to the one that has Persno = 2 as you said on the goal. Pasting your entire query would help to understand that also.
I was trying to understand your query, so I created a fake query for your original one:
To get the same results you would need a Left Join and not a Inner Join as a query with a subquery wouldn't exclude results from the outer table, so you would have something like this as the resulting Join query:
请使用:
Please use: