使用自连接时 MS Access 2010 中没有当前记录错误

发布于 2024-12-15 18:57:22 字数 708 浏览 2 评论 0原文

我正在用自联接替换子查询,以提高查询的性能。

旧的子查询是这样的:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2024-12-22 18:57:22

嗯,看起来您想要优化的这个查询是一个更大查询的一部分,并且对于您发布整个查询的问题很重要,因此它将有助于理解您的问题......

另外,从我所看到的将显示具有相同 AACode 的两行的 RAge2,而不仅仅是像您在目标中所说的那样显示 Persno = 2 的行。粘贴整个查询也将有助于理解这一点。

我试图理解您的查询,因此我为您的原始查询创建了一个查询:

SELECT
(SELECT FAge2.AgeCat
 FROM People AS FAge2
 WHERE FAge2.aacode = People.aacode 
   AND FAge2.PERSNO = 2) AS RAge2,
People.PersonId
FROM People

要获得相同的结果,您需要左连接而不是内连接,因为带有子查询的查询不会' t 从外表中排除结果,因此您将得到类似这样的结果 Join 查询:

SELECT 
 FAge2.AgeCat as RAge2,
 People.PersonID,
FROM People
Left JOIN People AS FAge2 ON (FAge2.aacode = People.aacode AND FAge2.PERSNO = 2)

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:

SELECT
(SELECT FAge2.AgeCat
 FROM People AS FAge2
 WHERE FAge2.aacode = People.aacode 
   AND FAge2.PERSNO = 2) AS RAge2,
People.PersonId
FROM People

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:

SELECT 
 FAge2.AgeCat as RAge2,
 People.PersonID,
FROM People
Left JOIN People AS FAge2 ON (FAge2.aacode = People.aacode AND FAge2.PERSNO = 2)
谈情不如逗狗 2024-12-22 18:57:22

请使用:

(SELECT [People].[AgeCat] FROM [People] INNER JOIN [People] AS P2 ON ([People].[aacode] = [P2].[aacode] AND [P2].[PERSNO] = 2)) AS RAge2

Please use:

(SELECT [People].[AgeCat] FROM [People] INNER JOIN [People] AS P2 ON ([People].[aacode] = [P2].[aacode] AND [P2].[PERSNO] = 2)) AS RAge2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文