2011-03-25 78 views
0

嗨請幫我 我需要查詢下面的例子 在兩個表日期字段相同的日期來更多次或一次我需要輸出的查詢只有一行每天不想重複的日子和我們要計算的其他支付和稅收在相同的兩個表mysql總額支付和groupby日期

前 table1的

payment tax date 

100 1.02 15-1-2010 

23 0.09 15-1-2010 

45 2.3 2-3-2011 

表2

pay pdate 

400 15-1-2010 

253 18-1-2010 

465 2-2-2011 

我需要這樣的

pay tax pdate 

523 1.02 15-1-2010 

465 0 2-2-2011 

45 2.3 2-3-2011 
+0

輸出你有外鍵連接表 – Gowri 2011-03-25 09:27:56

+0

不,我只是需要時間明智........我想基於沒有必要條件日期 – Mohan 2011-03-25 09:33:57

+0

行18-1-2010失蹤 – RichardTheKiwi 2011-03-25 09:35:57

回答

0
select sum(payment) pay, min(tax) tax, pdate 
from 
(
    select payment, tax, date 
    from table1 
    union all 
    select pay, 0, pdate 
    from table2 
) x 
group by date 
+0

select sum(mc_gross)as pay,ifnull(min(tax),0)as tax,payment_date from ( select mc_gross,tax,payment_date from table1 union all select totalamount,paydate from table2)x group by payment_date 它返回 #1222 - 使用的SELECT語句有不同數量的列 – Mohan 2011-03-25 09:44:27

+0

@莫漢 - 抱歉意味着在那裏添加0 – RichardTheKiwi 2011-03-25 09:55:44

0
select sum(payment) pay, sum(tax) tax, date pdate 
    from (select payment, tax, date 
      from table1 
     union all 
     select pay,0.0, pdate 
      from table2) t 
group by pdate;