2012-07-26 61 views
0

共12個表格(重複某些表格)。我必須從每個特定日期的表中獲取SUM(值)。我使用了UNION查詢,但它返回查詢中使用的第一個表的值。 剩餘的表格沒有任何返回。可以任何人幫助我。在這裏我的代碼。聯合查詢以獲取SUM值

$sel = mysql_query(" 
SELECT 
    SUM(collection_amount) AS cash_total 
FROM 
    collection_entry 
WHERE 
    date='$entered_date' 
    AND collection_type='DC' 
UNION 
SELECT 
    SUM(amt) AS cheque_redeposit_total 
FROM 
    cheque_redeposit 
WHERE 
    redeposited_on1 
    OR redeposited_on2='$entered_date' 
UNION 
SELECT 
    SUM(collection_amount) AS not_cleared_total 
FROM 
    collection_entry 
WHERE 
    cheque_status='not cleared' 
    AND date='$entered_date' 
UNION 
SELECT 
    SUM(collection_amt) AS route_collection_total 
FROM 
    route_collection 
WHERE 
    entered_date='$entered_date' 
UNION 
SELECT 
    SUM(amt) AS return_total 
FROM 
    cheque_return 
WHERE 
    return_date1 OR return_date2 OR return_date3='$entered_date' 
UNION 
SELECT 
    SUM(collection_amount) AS cheque_total 
FROM 
    collection_entry 
WHERE 
    collection_type='CC' 
    AND date='$entered_date' 
UNION 
SELECT 
    SUM(debit2) AS voucher_receipt_total 
FROM 
    voucher_posting 
WHERE 
    receipt_type='R' 
    AND date='$entered_date' 
UNION 
SELECT 
    SUM(credit2) AS voucher_payment_total 
FROM 
    voucher_posting 
WHERE 
    receipt_type='P' 
    AND date='$entered_date' 
UNION 
SELECT 
    SUM(amt) AS others_total 
FROM 
    others_remittance 
WHERE 
    entered_date='$entered_date' 
UNION 
SELECT 
    SUM(amt) AS short_total 
FROM 
    short_remittance 
WHERE 
    entered_date='$entered_date' 
UNION 
SELECT 
    SUM(amount) AS more_paid 
FROM 
    difference 
WHERE 
    entered_date='$entered_date' 
    and paid_type='more' 
UNION 
SELECT 
    SUM(amount) AS unpaid 
FROM 
    difference 
WHERE 
    entered_date='$entered_date' 
    and paid_type='unpaid'"); 
while($row=mysql_fetch_array($sel)) 
{ 
    $cash_total=$row['cash_total']; 
    $cheque_redeposit_total=$row['cheque_redeposit_total']; 
    $not_cleared_total=$row['not_cleared_total']; 
    $route_collection_total=$row['route_collection_total']; 
    $return_total=$row['return_total']; 
    $cheque_total=$row['cheque_total']; 
    $voucher_receipt_total=$row['voucher_receipt_total']; 
    $voucher_payment_total=$row['voucher_payment_total']; 
    $others_total=$row['others_total']; 
    $short_total=$row['short_total']; 
    $more_paid=$row['more_paid']; 
    $unpaid=$row['unpaid']; 
    $net_total = (($cash_total + $route_collection_total) - $return_total); 
} 

回答

1

UNION只是將行追加到彼此。所以在你的情況下,你只需按照行的方式得到你的款項清單。

  • [value for cash_total]
  • [value for cheque_redeposit_total]
  • [value for not_cleared_total]
  • ...

如果你真的要得到一個行中的所有數據,你可以使用這樣的事情:

SELECT * FROM 
(SELECT SUM(collection_amount) AS cash_total FROM collection_entry WHERE date='$entered_date' AND collection_type='DC') as t1, 
(SELECT SUM(amt) AS cheque_redeposit_total FROM cheque_redeposit WHERE redeposited_on1 OR redeposited_on2='$entered_date') AS t2, 
(SELECT SUM(collection_amount) AS not_cleared_total FROM collection_entry WHERE cheque_status='not cleared' AND date='$entered_date') AS t3, 
(SELECT SUM(collection_amt) AS route_collection_total FROM route_collection WHERE entered_date='$entered_date') AS t4, 
... 
+0

感謝您的回覆Sirko ...我得到了它... – 2012-07-26 10:16:53