mysql中选择性sum求和及展示问题
发布网友
发布时间:2022-04-26 09:57
我来回答
共2个回答
热心网友
时间:2022-04-07 16:41
你这个情况一条语句实现不了,只能分两次。
求转入之和:
SELECT SUM(金额) FROM tableName WHERE 业务类型 = 0 OR 业务类型 = 2 GROUP BY 用户id;
求转出之和:
SELECT SUM(金额) FROM tableName WHERE 业务类型 = 1 OR 业务类型 = 3 GOUP BY 用户id;
热心网友
时间:2022-04-07 17:59
1,你的(结果表)有错误呀,id1和id2怎么能有转出之和呢
2,分组查的太繁了不想写了
下面是子查询最多就这样了拿去爽
create table text_09(
id int unsigned primary key auto_increment,
id01 int unsigned,
p int unsigned,
m decimal(10,2)
);
insert into text_09 values
(0,1,1,100),
(0,1,2,200),
(0,2,0,100),
(0,2,1,100),
(0,2,2,200),
(0,2,3,100);
mysql> select * from text_09;
+----+------+------+--------+
| id | id01 | p | m |
+----+------+------+--------+
| 1 | 1 | 0 | 100.00 |
| 2 | 1 | 1 | 100.00 |
| 3 | 1 | 2 | 200.00 |
| 4 | 2 | 0 | 100.00 |
| 5 | 2 | 1 | 100.00 |
| 6 | 2 | 2 | 200.00 |
| 7 | 2 | 3 | 100.00 |
+----+------+------+--------+
select * from
(select sum(m) as 转入 from text_09 where p in (0,1)) as a
inner join
(select sum(m) as 转出 from text_09 where p in (2,3)) as b;