2015-12-13 176 views
1

如何連接下面的兩條select語句以作爲一條語句使用?我希望結果出現在一張表中。謝謝你的幫助。加入兩條包含一個WHERE子句的MySQL SELECT語句

首先聲明 -

SELECT Account_ID, SUM(Profit_Loss) AS Starting_Balance 
FROM client_ledger_history 
WHERE Summary = 'Cash In' 
GROUP BY Account_ID WITH ROLLUP 

第二條語句 -

SELECT 
    client_ig_client_list.Account_ID, 
    client_ig_client_list.`Name`, 
    Share_Status, 
    Forex_Status, 
    Index_Status, 
    Share_Weighting, 
    Forex_Weighting, 
    Index_Weighting, 
    SUM(
     client_ledger_history.Profit_Loss 
    ) AS Current_Balance 
FROM 
    client_ledger_history 
LEFT JOIN client_ig_client_list ON client_ig_client_list.Account_ID = client_ledger_history.Account_ID 
GROUP BY 
    Account_ID WITH ROLLUP 
+0

如果您提供的樣本數據你的問題會比較清楚和預期的結果。 –

回答

0

你應該做一個加入到嵌套表

SELECT 
client_ig_client_list.Account_ID, 
Starting_Balance, 
client_ig_client_list.`Name`, 
Share_Status, 
Forex_Status, 
Index_Status, 
Share_Weighting, 
Forex_Weighting, 
Index_Weighting, 
SUM(client_ledger_history.Profit_Loss) AS Current_Balance 
FROM 
    client_ledger_history LEFT JOIN client_ig_client_list ON client_ig_client_list.Account_ID = client_ledger_history.Account_ID 
LEFT JOIN 
(SELECT Account_ID, SUM(Profit_Loss) AS Starting_Balance 
FROM client_ledger_history WHERE Summary = 'Cash In' GROUP BY Account_ID WITH ROLLUP) as client_ledger_aggreagated_history 
ON client_ledger_aggreagated_history.Account_ID = client_ledger_history.Account_ID 
    GROUP BY Account_ID WITH ROLLUP 
+1

感謝@MrMush但是當我嘗試您的語句時,我收到以下錯誤。 [錯誤] 1054 - '字段列表'中的未知列'client_ledger_aggreagated_history.Profit_Loss' –

+0

糟糕,修復它,請重試。 –

+1

這就是完美@Mr!謝謝你的幫助。 –