这就是你应该用的方法
group by
汇总:
select users, ref,
count(`add`) `add`,
count(sub) sub,
count(mul) mul,
sum(marks) total
from tablename
group by users, ref
见
demo
结果:
| users | ref | add | sub | mul | total |
| ----- | --- | --- | --- | --- | ----- |
| u1 | 20 | 3 | 2 | 1 | 270 |
| u2 | 15 | 2 | 1 | 0 | 70 |
| u3 | 20 | 1 | 3 | 1 | 145 |