我正在使用此查询:
SELECT `projects`.*, (SELECT SUM(`amount`) FROM `accountprojectspayment` WHERE `projects_id` = `projects`.`id`) AS `payed` FROM `projects` INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id` HAVING `bids`.`amount` >= `payed`
我收到此错误:'having clause
中的未知列'bids.amount'但如果我将代码更改为:
SELECT `projects`.*, `bids`.`amount`, (SELECT SUM(`amount`) FROM `accountprojectspayment` WHERE `projects_id` = `projects`.`id`) AS `payed` FROM `projects` INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id` HAVING `bids`.`amount` >= `payed`
问题得到解决,但我不想使用选择bids
。amount
答案 0 :(得分:4)
使用派生表格,例如像这样的东西:
SELECT `DT1`.*
FROM (SELECT `projects`.*,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`) AS `DT1`
INNER JOIN `bids` ON `bids`.`id` = `DT1`.`bids_id`
WHERE `bids`.`amount` >= `DT1`.`payed`;