Jump to content

MySql Syntax


Jonob

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.