我有这个查询字符串......
select i.invoiceid, i.date, i.total, i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid, p.date, p.invoice, p.amount from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date
当我试着这个......
<?php
echo $clientArrayInvoice[1]['paymentid'];
?>
当我在$ clientArrayInvoice上执行print_r时,我得不到任何结果我得到了这个
Array ( [0] => Array ( [invoiceid] => 1 [date] => 2012-04-12 [total] => 602.29 [remainingbalance] => 300.96 ) [1] => Array ( [invoiceid] => 1 [date] => 2012-04-27 [total] => 1.00 [remainingbalance] => 301.33 ) )
我理解为什么要这样做,但是如何让列为{4}}而不是paymentid
来表示返回的结果。所以我可以确定什么是invoiceid
什么是paymentid
我希望这是有道理的。
答案 0 :(得分:2)
select i.invoiceid as transactionid, i.date, i.total,
i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance,
'invoice' as transaction_type
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid as transactionid, p.date, p.invoice, p.amount, 'payment' as transaction_type
from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date