2015-12-13 135 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!谢谢你的帮助。 –