1

私はこのクエリ文字列を持っています...

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 ) )

なぜこれを行うのかは理解していますが、返された結果のpaymentid代わりに列を表示するにはどうすればよいですか。何がで何invoiceidが であるかを判断できるので、これが理にかなっていることを願っています。paymentidinvoiceid

4

1 に答える 1

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
于 2012-04-27T21:29:53.493 に答える