我在MySql中运行以下查询
SELECT count(*)
FROM `pitches` AS `Pitch`
INNER JOIN `business_sectors_pitches` AS `BusinessSector`
ON (`BusinessSector`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `investments` AS `Investment`
ON (`Investment`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `users` AS `User`
ON (`Pitch`.`user_id` = `User`.`id`)
LEFT JOIN `loan_pitches` AS `LoanPitch`
ON (`LoanPitch`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `equity_pitches` AS `EquityPitch`
ON (`EquityPitch`.`pitch_id` = `Pitch`.`id`)
WHERE `Pitch`.`status` = 1
AND `Pitch`.`required_funding` >= 0
AND `Pitch`.`required_funding` <= 1500000
GROUP BY `Pitch`.`id`
having ( ( sum(`Investment`.`amount`) / `Pitch`.`required_funding` >= 0
and sum(`Investment`.`amount`) / `Pitch`.`required_funding` <= 1
) or sum(`Investment`.`amount`) / `Pitch`.`required_funding` is null
)
and count(`Investment`.`id`) >= 0
and count(`Investment`.`id`) <= 200 ;
并获得以下错误
ERROR 1054 (42S22): Unknown column 'Pitch.required_funding' in 'having clause'
我发现需要替换的地方,但我无法使其有效。
这段代码是从我的Cakephp代码生成的,但我不能让它在mysql客户端中运行。
根据@GolezTrol评论第二次尝试
SELECT count(*)
FROM `pitches` AS `Pitch`
INNER JOIN `business_sectors_pitches` AS `BusinessSector`
ON (`BusinessSector`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `investments` AS `Investment`
ON (`Investment`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `users` AS `User`
ON (`Pitch`.`user_id` = `User`.`id`)
LEFT JOIN `loan_pitches` AS `LoanPitch`
ON (`LoanPitch`.`pitch_id` = `Pitch`.`id`)
LEFT JOIN `equity_pitches` AS `EquityPitch`
ON (`EquityPitch`.`pitch_id` = `Pitch`.`id`)
WHERE `Pitch`.`status` = 1
AND `Pitch`.`required_funding` >= 0
AND `Pitch`.`required_funding` <= 1500000
AND ( ((select sum(amount) from investments group by pitch_id) / `Pitch`.`required_funding` >= 0
AND (select sum(amount) from investments group by pitch_id) / `Pitch`.`required_funding` <= 1)
OR (select sum(amount) from investments group by pitch_id) / `Pitch`.`required_funding` is null);