Jump to content

Recommended Posts

Hey Guys,

So here's the thing, I need to draw up an outstanding amount based on 5 tables information:

credit_notes: credit data

- credit_note_products

invoices: debit data

- invoice_products

payments: credit data

 

So this is what I have:

SELECT
(SELECT ROUND(SUM(((sell_price*quantity)-((sell_price*quantity)*(discount/100)))*1.14),2)
FROM invoice_products 
INNER JOIN invoices ON (invoices.id=invoice_products.invoice_id)
WHERE invoice_id=invoices.id and customer_id=127) AS outstanding,
(SELECT SUM(credit_amount*quantity) 
FROM credit_note_products 
INNER JOIN credit_notes ON (credit_notes.id=credit_note_products.credit_note_id)
WHERE customer_id=127) AS credit_notes,
(SELECT	amount
FROM customer_payments 
WHERE customer_id=127) AS payments,
outstanding-credit_notes-payments AS balance_brought_forward

 

Query works fine if I leave out the last line above. As soon as I try and subtract the fields from each other- thats where the problem lies...

Link to comment
https://forums.phpfreaks.com/topic/132673-unknown-fields-from-subqueries/
Share on other sites

What is the error message?

 

Oh, and you can't use aliases you're just defined in defining another column. So you'd need:

(SELECT ROUND(SUM(((sell_price*quantity)-((sell_price*quantity)*(discount/100)))*1.14),2)
FROM invoice_products
INNER JOIN invoices ON (invoices.id=invoice_products.invoice_id)
WHERE invoice_id=invoices.id and customer_id=127) - (SELECT SUM(credit_amount*quantity)
FROM credit_note_products
INNER JOIN credit_notes ON (credit_notes.id=credit_note_products.credit_note_id)
WHERE customer_id=127) - (SELECT   amount
FROM customer_payments
WHERE customer_id=127) AS balance_brought_forward

 

Which obviously doesn't make much sense. It's better to calculate balance_brought_forward in your PHP script.

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.