我有两个表几乎相同的列,我想合并它们并按日期分组
表1:order_payments_detail
| payment_by | amount( as Debit in result) | added_on |
| Ali | 1000 | 2014-09-21 |
| Aslam | 2000 | 2014-09-25 |
| Akram | 4000 | 2014-09-28 |
表2:订单
| cust_name | amount( as credit in result)| added_on |
| Shop1 | 1000 | 2014-09-22 |
| Shop2 | 2000 | 2014-09-26 |
| Shop3 | 4000 | 2014-09-29 |
结果将是这样的
| particulars| debit | credit | added_on |
| Ali | 1000 | null | 2014-09-21 |
| Shop1 | null | 1000 | 2014-09-22 |
| Aslam | 2000 | null | 2014-09-25 |
| Shop2 | null | 2000 | 2014-09-26 |
| Akram | 4000 | null | 2014-09-28 |
| Shop3 | null | 4000 | 2014-09-29 |
答案 0 :(得分:1)
您可以使用union all
:
select payment_by, amount as debit, NULL as credit, added_on
from order_payments_detail
union all
select cust_name, NULL, amount, added_on
from orders
order by added_on;
按“按日期分组”,我认为你的意思是“按日期排序”。