mysql - MYSQL Group By Query,根据where条件减去数字
问题描述
我有一个事务表(“TableA”),在 TableA 中有这些字段app_id、user_id、points、tr_type、description。所以在事务中有加减tr_type是有的。现在我想根据 tr_type 获取 user_id 的 total_points (points = added-subtract)。为此我需要如何编写 MySQL 查询>
对于类型是添加或子。
所以现在我想要这 3 个字段的 GroupBY --> app_id, user_id, points
解决方案
我怀疑你想要条件聚合:
select user_id,
sum(case when tr_type = 'add' then points
when tr_type = 'sub' then - points
end)
from t
group by user_id;
推荐阅读
- ruby-on-rails - 这个表的索引在哪里
- javascript - iOS Web 应用程序在切换到银行 ID 时失去连接
- python - Python关闭证书验证
- python - 如何从字典中打印出格式良好的表格
- java - Android Studio - PagerView2(With Fragment) power 2
- android - 如何正确提取手机游戏文件?
- anglesharp - AngleSharp OpenAsync 返回令人困惑的错误?没有网络的时候
- sql-server - 使用 Azure Active Directory 连接到 MSSQL 服务器的 JDBC
- java - 如何在 java 中将 SSL 客户端证书与 jersey 客户端一起使用
- botium-box - Botium:- 使用 botium 和 webdriverIO 进行聊天机器人测试