2014-10-05 71 views
1

得到與分組兩個表的數據,我有兩個表幾乎相同的列,我想將它們合併和按日期
MySQL的:通過日期

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

回答

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; 

通過使用「按日期分組」,我認爲你真的是「按日期排序」。

+0

是的,我的意思是按日期排序thankyou非常多 – 2014-10-05 22:05:34