这是答案:
创建表:
CREATE TABLE table_name (
m int,
win int,
lose int,
qty int);
插入数据:
INSERT INTO table_name (m, win, lose, qty)
values
(1, 100, 201, 10),
(2, 201, 100, 05),
(3, 100, 201, 05),
(4, 302, 100, 05);
查询:
SELECT id, sum(won), sum(lost) from (
SELECT win as id, SUM(qty) as won, 0 as lost
FROM table_name W GROUP BY win
UNION
SELECT lose as id, 0 as won, SUM(qty) as lost
FROM table_name W GROUP BY lose
) sums
GROUP BY ID
结果:
+------+----------+-----------+
| id | sum(won) | sum(lost) |
+------+----------+-----------+
| 100 | 15 | 10 |
| 201 | 5 | 15 |
| 302 | 5 | 0 |
+------+----------+-----------+