查询在工作台中工作,但在php中不起作用

时间:2013-12-18 15:54:14

标签: php mysql

我正在尝试根据where语句中的子查询为变量赋值。问题是它在php中不起作用,但在工作台中查询运行正常。我没有在php中得到任何错误,它返回正确的付费ID,但变量字段返回空。

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

1 个答案:

答案 0 :(得分:3)

为什么不尝试使用JOIN而不是子查询/变量?

这样的事情:

SELECT pay_id, (pay_amount - COALESCE(SUM(applied_amount), 0)) AS amount_available
FROM tblpayments payments
LEFT JOIN tblxref_pmt_chg xref ON xref_pay_id = payments.pay_id
WHERE customer_id = 9
GROUP BY payments.pay_id
HAVING amount_available > 0