在子查询中使用主查询中的列时出错

发布于 2024-12-15 04:37:38 字数 673 浏览 1 评论 0原文

我不是一个高级的 mysql 用户,所以你必须忍受我的这一点。

我尝试在其中一个子查询中使用 cref 变量,但收到一条错误,指出 cref 列不存在。如果我取出子查询,它将显示该列,因此该列肯定存在。

另外,如果有任何其他错误,请注意:)

SELECT DISTINCT
  (contractorsRef) AS cref,
  RIDGROUP AS ridg,
  (
    SELECT count(*) FROM (
      SELECT DISTINCT subcontractorRef
      FROM request
      INNER JOIN request_config
        ON request_config.RIDGROUP = request.RIDGROUP
      WHERE request_config.contractorsRef = cref  --### ERROR HERE
        AND currenttaxyear =2011
        AND weekno =31
  ) AS xx
) as xxx
FROM request_config
WHERE currenttaxyear =2011
  AND weekno =32
  AND contractorsRef <>132

I'm not an advanced mysql user, so you will have to bear with me on this.

I am trying to use the cref variable in one of the subqueries, but I am getting an error that the cref column does not exist. If I take the subquery out it will display the column, so the column definitely exists.

Also if there are any other mistakes, would appreciate the heads up :)

SELECT DISTINCT
  (contractorsRef) AS cref,
  RIDGROUP AS ridg,
  (
    SELECT count(*) FROM (
      SELECT DISTINCT subcontractorRef
      FROM request
      INNER JOIN request_config
        ON request_config.RIDGROUP = request.RIDGROUP
      WHERE request_config.contractorsRef = cref  --### ERROR HERE
        AND currenttaxyear =2011
        AND weekno =31
  ) AS xx
) as xxx
FROM request_config
WHERE currenttaxyear =2011
  AND weekno =32
  AND contractorsRef <>132

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝戈者 2024-12-22 04:37:38

限定内部选择中的引用

SELECT DISTINCT (contractorsRef) AS cref, RIDGROUP AS ridg, 
    (select count(DISTINCT subcontractorRef)
    FROM request
    INNER JOIN request_config ON request_config.RIDGROUP = request.RIDGROUP
    WHERE request_config.contractorsRef = outer_config.contractorsRef 
    AND currenttaxyear =2011
    AND weekno =31) AS xxx
FROM request_config outer_config
WHERE currenttaxyear =2011
AND weekno =32
AND contractorsRef <>132

试试这个(请注意,我将外部选择中的 request_config 标记为 outer_config ,并使用标签EDIT
请再试一次,我从子选择中删除了 1 个不必要的层

try this (note that I labeled the request_config in the outer select as outer_config and I qualified the reference in the inner select with the label

SELECT DISTINCT (contractorsRef) AS cref, RIDGROUP AS ridg, 
    (select count(DISTINCT subcontractorRef)
    FROM request
    INNER JOIN request_config ON request_config.RIDGROUP = request.RIDGROUP
    WHERE request_config.contractorsRef = outer_config.contractorsRef 
    AND currenttaxyear =2011
    AND weekno =31) AS xxx
FROM request_config outer_config
WHERE currenttaxyear =2011
AND weekno =32
AND contractorsRef <>132

EDIT
pls try again, I removed 1 unnecessary layer from the subselect

耶耶耶 2024-12-22 04:37:38

首先执行子查询,并将其结果用作外部查询的输入。因此,当您在外部查询中创建 coulmn cref 时,在执行子查询时它还不存在。

您也许可以通过合并两个查询来解决您的问题。从子查询开始,然后使用 request_config 加入。然后添加适当的条件和适当的列

Subqueries are executed first and their results are used as input to the outer query. So while you are creating the coulmn cref in your outer query, it does not yet exist when your subquery is being executed.

You can probably solve your problem by merging the two queries. Start with your subquery, and join with request_config. Then add the appropriate conditions, and the appropriate columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文