Mysql 需要返回包含两个不同where子句的两组数据
我有一张记录交易的表格 该表设置为:Mysql 需要返回包含两个不同where子句的两组数据,mysql,sql,Mysql,Sql,我有一张记录交易的表格 该表设置为: transactions: id, account_id, budget_id, points, type 我需要返回每个budget_id的类型为'allocation'的点数和类型为'issue'的点数和 我知道如何在一个查询中处理每一个问题,但不能同时处理这两个问题 预期结果集: budget_id allocated issued 434 200000 100 242 100000
transactions:
id, account_id, budget_id, points, type
我需要返回每个budget_id的类型为'allocation'的点数和类型为'issue'的点数和
我知道如何在一个查询中处理每一个问题,但不能同时处理这两个问题
预期结果集:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
只有满足特定条件时,才能使用事例求和。字段列表中的子查询或子查询的联接.g。若要开始,请选择预算id、A.分配的所有金额、I.从交易中发出的iss金额内部联接选择SUMpoints作为交易中的所有金额,其中type='Allocation'作为内部联接选择SUMpoints作为交易中的所有金额,其中type='Issue'作为内部联接选择SUMpoints作为交易中的所有金额I@scrowler-将起作用,但是如果分组相同,您可以使用case进行条件求和。嗨,Brad。以下任一答案对您有帮助吗?如果是,请考虑将其中一个标记为接受。
SELECT budget_id,
SUM(IF(type = 'allocation', points, 0)) AS allocated,
SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
select budget_ID,
sum(case when type = 'allocated' then points else 0 end) as allocated,
sum(case when type = 'issued' then points else 0 end) as issued
..rest of your query...
group by budget_ID