2012-01-18 54 views
0
select 
     IFNULL(sum(invoice0_.INV_AMT),0) as col_0_0_,IFNULL(sum(invoice1_.INV_AMT),0) as col_0_0_ 
    from 
     hrmanager.invoice invoice0_, 
     hrmanager.invoice invoice1_ 
    where 
     invoice0_.FROM_LEDGER=1 
     **or** invoice1_.TO_LEDGER=1 
     and (
      invoice0_.INV_DATE between '1900-12-20' and '2012-01-30' 
     ) 
     and invoice0_.ACTIVE='Y' 
     and invoice0_.COMP_ID=2 
     and invoice1_.COMP_ID=2 
     and invoice0_.INV_TYPE='CLIENT' 
     and invoice1_.INV_TYPE='CLIENT'; 

在這裏,我要選擇所有from_ledger = 1和下一列的量的總和應顯示所有to_ledger = 1的量的總和,但這裏給了和/或在條件檢索相同的數據,在Db這裏從分類帳= 1然後結果是7000和toledger = 1然後它將0但上述查詢檢索兩列是相同的值,如0或7000MySQL查詢使用檢索值if語句

回答

0

它看起來像您的查詢有點混亂。您正在從同一個表執行兩次查詢,但沒有會導致笛卡爾結果的JOIN條件。我認爲你要找的是你的表「發票」有兩列......「To_Ledger」和「From_Ledger」,「INV_AMT」和其他一些標準領域......這應該讓你更接近你的答案。

select 
     sum(if(inv.From_Ledger = 1, inv.Inv_Amt, 0)) as FromInvoiceAmounts, 
     sum(if(inv.To_Ledger = 1, inv.Inv_Amt, 0)) as ToInvoiceAmounts 
    from 
     hrmanager.invoice inv 
    where 
      1 in (inv.From_Ledger, inv.To_Ledger) 
     AND inv.inv_date between '1900-12-20' and '2012-01-30' 
     and inv.Active = 'Y' 
     and inv.Comp_ID = 2 
     and inv.Inv_Type = 'CLIENT'