我想按日期从下表中选择数据
date btype amount
1-2-2017 income 1000
1-2-2017 income 200
1-2-2017 Expense 100
1-2-2017 Expense 200
2-2-2017 income 1000
2-2-2017 income 500
2-2-2017 Expense 1000
3-2-2017 income 2000
3-2-2017 Expense 500
所以我的结果应该是这样的。
date Income Expense
1-2-2017 1200 300
2-2-2017 1500 1000
3-2-2017 2000 500
有人会指导我如何编写 SQL 查询来实现此行为吗!
最佳答案
这是相当简单的 SQL,您应该能够通过一些 google 自行了解:
select date
,sum(if(btype = 'income', amount, 0)) as Income
,sum(if(btype = 'expense', amount, 0)) as Expense
from table
group by date
关于php - 如何选择按日期分组的 SQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42159390/