账单表
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
账单表
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
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
您需要完整的外部联接来获取有账单但没有付款的天数和有付款但没有账单的天数的行。
试试这个
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
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
查询应该是这样的,
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
你可以试试这段代码。
SELECT concat(`Bill_date`,' ',`Total`) FROM `tblname`