gpt4 book ai didi

mysql - 将两个 MYSQL WHERE 查询连接在一起

转载 作者:行者123 更新时间:2023-11-30 22:29:40 26 4
gpt4 key购买 nike

我想将 2 个查询的结果连接在一起,这样我最终得到 3 列,第一列是 MONTH,第二列是 WITHDRAWALS,第三列是 DEPOSITS。

查询 1:

SELECT
DATE_FORMAT(Time, "%m-%Y") AS MONTH,
SUM(Profit_Loss) AS DEPOSITS
FROM
`deposits_withdrawals_view`
WHERE
Summary = 'Cash In'
GROUP BY
DATE_FORMAT(Time, "%m-%Y")

查询 2:

SELECT
DATE_FORMAT(Time, "%m-%Y") AS MONTH,
SUM(Profit_Loss) AS WITHDRAWALS
FROM
`deposits_withdrawals_view`
WHERE
Summary = 'Cash Out'
GROUP BY
DATE_FORMAT(Time, "%m-%Y")

两者都单独工作,但如何在 1 个查询中将它们连接在一起?

感谢您的帮助。

最佳答案

你可以为此做条件聚合

select
DATE_FORMAT(Time, "%m-%Y") AS MONTH,
sum(case when Summary='Cash In' then Profit_Loss else 0 end) as DEPOSITS,
sum(case when Summary='Cash Out' then Profit_Loss else 0 end) as WITHDRAWALS
FROM
`deposits_withdrawals_view`
GROUP BY MONTH

关于mysql - 将两个 MYSQL WHERE 查询连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34198630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com