Jonob Posted October 28, 2008 Share Posted October 28, 2008 Hi all, New to this php and mysql thing, but getting there slowly but surely. I am running mysql 5.05 I have a query as follows, which works 100%: SELECT i.*,c.name, (SELECT SUM(amount_inc) FROM income_detail WHERE invoice_id = i.invoice_id) AS paid FROM invoice i LEFT JOIN counterpart c on i.counterpart_id = c.counterpart_id WHERE i.is_deleted = 'N' AND i.company_id = 1 AND i.counterpart_id = 1 ORDER BY i.date However, what I would like to do, is limit the results to cases where i.amount_inc>paid I tried the following, but it doesnt like the way that I referenced the 'paid' part of the code. SELECT i.*,c.name, (SELECT SUM(amount_inc) FROM income_detail WHERE invoice_id = i.invoice_id) AS paid FROM invoice i LEFT JOIN counterpart c on i.counterpart_id = c.counterpart_id WHERE i.is_deleted = 'N' AND i.company_id = 1 AND i.counterpart_id = 1 AND i.amount_inc > paid --This is the new line ORDER BY i.date What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/130402-mysql-syntax/ Share on other sites More sharing options...
revraz Posted October 28, 2008 Share Posted October 28, 2008 Does it actually return an error? Quote Link to comment https://forums.phpfreaks.com/topic/130402-mysql-syntax/#findComment-676578 Share on other sites More sharing options...
waterssaz Posted October 28, 2008 Share Posted October 28, 2008 The following should work: SELECT i.*,c.name, (SELECT SUM(amount_inc) FROM income_detail WHERE invoice_id = i.invoice_id) AS paid FROM invoice i LEFT JOIN counterpart c on i.counterpart_id = c.counterpart_id WHERE i.is_deleted = 'N' AND i.company_id = 1 AND i.counterpart_id = 1 AND i.amount_inc > (SELECT SUM(amount_inc) FROM income_detail WHERE invoice_id = i.invoice_id) ORDER BY i.date You cannot simply reference > 'paid' as this is simply an alias and is not a variable to store data Quote Link to comment https://forums.phpfreaks.com/topic/130402-mysql-syntax/#findComment-676703 Share on other sites More sharing options...
fenway Posted October 30, 2008 Share Posted October 30, 2008 Not without a HAVING clause. Quote Link to comment https://forums.phpfreaks.com/topic/130402-mysql-syntax/#findComment-678525 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.