我有一个这样的sql查询......
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT ifnull(sum(total), 0)
FROM invoice i
WHERE i.client = c.clientid AND i.isdeleted = 0) -
(SELECT ifnull(sum(p.amount), 0)
FROM payment p
INNER JOIN invoice i ON p.invoice = i.invoiceid
WHERE i.client = c.clientid and i.isdeleted = 0) as balance,
CASE c.isactive+0 WHEN '1' THEN 'Stop'
ELSE 'Start' END as Active
FROM client c
ORDER BY clientname
这工作正常,没有错误,但请注意这部分....
(SELECT ifnull(sum(total), 0)
FROM invoice i
WHERE i.client = c.clientid AND i.isdeleted = 0) -(SELECT ifnull(sum(p.amount), 0)
FROM payment p
INNER JOIN invoice i ON p.invoice = i.invoiceid
WHERE i.client = c.clientid AND i.isdeleted = 0) as balance
我写了一个PHP脚本...
if($remaining < 0){
$remaining = $row['total'];
}else{
$remaining = $remaining + $row['total'];
}
我想要做的是将我在 PHP 中编写的内容合并到我的 SQL 查询中,但我之前从未使用 if 语句编写过 SQL 查询(如果允许的话)。如何将我的 PHP 脚本合并到我的 SQL 查询中?有什么建议么?