2

Im using this query:

    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`

i get this error: Unknown column 'bids.amount' in 'having clause

But if i change the code to this:

    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`

the problem get solved but i do not want to use Select bids.amount

1 Answer 1

4

Use a derived table e.g. something like this:

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`;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.