1

账单表

Bill_date   Total
28/10/13    10000
27/10/13    5000

付款表

Paid_on    amount
28/10/13   5000
30/10/13   10000

预期产出

date        Credit    debit
27/10/13    5000    
28/10/13    10000     5000
30/10/13              10000
4

5 回答 5

1
select b.bill_date, sum(b.total), sum(p.amount)
from bills b full outer join payments p on b.bill_date=p.paid_on
group by b.bill_date
order by b.bill_date

您需要完整的外部联接来获取有账单但没有付款的天数和有付款但没有账单的天数的行。

于 2013-10-28T07:22:33.830 回答
1

试试这个

SELECT t1.bill_date, t1.total as Credit, t2.total as debit  FROM Bills t1
LEFT JOIN Payments t2 ON t1.bill_date= t2.bill_date
UNION
SELECT t2.bill_date, t1.total as Credit, t2.total as debit FROM Bills t1
RIGHT JOIN Payments t2 ON t1.bill_date= t2.bill_date
于 2013-10-28T07:29:36.433 回答
0
SELECT t1.bill_date as `date`, t1.Total as Credit, t2.amount as debit  FROM Bills t1
LEFT JOIN Payments t2 ON t1.bill_date= t2.Paid_on
UNION
SELECT t2.Paid_on as `date`, t1.Total as Credit, t2.amount as debit FROM Bills t1
Right JOIN Payments t2 ON t1.bill_date <> t2.Paid_on
UNION
SELECT t1.bill_date as `date`, t1.Total as Credit, t2.amount as debit  FROM Bills t1
LEFT JOIN Payments t2 ON t1.bill_date <> t2.Paid_on
于 2013-10-28T07:32:54.450 回答
0

查询应该是这样的,

SELECT t1.bill_date, t1.total AS Credit, t2.amount AS debit
FROM Bills t1
LEFT JOIN Payments t2 ON t1.bill_date = t2.paid_on
UNION
SELECT t2.paid_on, t1.total AS Credit, t2.amount AS debit
FROM Bills t1
RIGHT JOIN Payments t2 ON t1.bill_date = t2.paid_on
于 2013-10-28T09:03:03.147 回答
-2

你可以试试这段代码。

  SELECT concat(`Bill_date`,' ',`Total`) FROM `tblname`
于 2013-10-28T07:14:21.780 回答