在子查询中使用主查询中的列时出错
我不是一个高级的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
限定内部选择中的引用
试试这个(请注意,我将外部选择中的
request_config
标记为outer_config
,并使用标签EDIT请再试一次,我从子选择中删除了 1 个不必要的层
try this (note that I labeled the
request_config
in the outer select asouter_config
and I qualified the reference in the inner select with the labelEDIT
pls try again, I removed 1 unnecessary layer from the subselect
首先执行子查询,并将其结果用作外部查询的输入。因此,当您在外部查询中创建 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