本文介紹了需要使用兩個不同的 where 子句返回兩組數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一張記錄交易記錄的表格.
I have a table that keeps track of transactions.
表格設置為:
transactions:
id, account_id, budget_id, points, type
我需要返回每個budget_id 的點總和,其中type = 'allocation' 和點總和,其中type = 'issue'
I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'
我知道如何在一個查詢中執行每個操作,但不能同時執行.
I know how to do each, but not both in one query.
預期結果集:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
推薦答案
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
這篇關于需要使用兩個不同的 where 子句返回兩組數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!