I'm trying to assign a variable a value based on a subquery in the where statement. The problem is that it doesn't work in php but in the workbench the query runs fine. I don't get any errors in php and it returns the correct pay ids but the variable field returns empty.
SELECT pay_id, @available AS amount_available
FROM tblpayments payments
WHERE customer_id = 9
AND (
@available := (pay_amount - (
SELECT if(sum(applied_amount) IS NULL, 0, sum(applied_amount))
FROM tblxref_pmt_chg xref WHERE xref_pay_id = payments.pay_id
))
) > 0
IFNULL(sum(applied_amount), 0):-)COALESCE(SUM(applied_amount), 0)(SQL standard, whereasIFNULL()is MySQL specific).COALESCE. It's actually got another cool feature too. It accepts infinite parameters and will return the 1st non-NULLone! :-D@available) here? Maybe you could convert this into a JOIN?