需要用两个不同的WHERE子句返回两组数据我有一张能记录交易的桌子。表的设置如下:transactions:id, account_id, budget_id, points, type我需要返回每个预算_id中类型=“分配”的点数和类型=“发布”的点数之和。我知道如何做到每一个,但不是同时在一个查询。预期结果集:budget_id allocated issued 434 200000 100
242 100000 5020
621 45000 3940
2 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
SELECT budget_id, SUM(IF(type = 'allocation', points, 0)) AS allocated, SUM(IF(type = 'issue', points, 0)) AS issuedFROM transactionsGROUP BY budget_id
哈士奇WWW
TA贡献1799条经验 获得超6个赞
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
添加回答
举报
0/150
提交
取消