mysql - 连接两个包含一个 WHERE 子句的 MySQL SELECT 语句

标签 mysql join where-clause

如何将下面的两个 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

最佳答案

您应该连接到嵌套表中

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

关于mysql - 连接两个包含一个 WHERE 子句的 MySQL SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250904/

相关文章:

python - Django 中的多模型对象继承

php - 比较时间时的 Laravel 问题 - 日期格式

mysql - 如果条件参数为空,则选择所有行

php - 用 3 个表计算项目

MySql - 获取值为 1 的列名称

php - Mysql查询和条件where子句

c# - 向数据集添加一列 C#

MySQL查询日期时间默认值

java - 从java servlet以正确的格式将数据插入到mysql表中

R data.table 滚动连接 "mult"没有按预期工作