我不是一个高级的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
答案 0 :(得分:0)
试试这个(请注意,我将外部选择中的request_config
标记为outer_config
,并使用标签
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
修改强> 请再试一次,我从子选择中删除了1个不必要的图层
答案 1 :(得分:0)
首先执行子查询,并将其结果用作外部查询的输入。因此,当 在您的外部查询中创建coulmn cref
时,当您的子查询被执行时,尚不存在。
您可以通过合并两个查询来解决问题。从您的子查询开始,然后加入request_config
。然后添加适当的条件和相应的列