我在表格中有这样的数据:
====================
class | in | out
====================
1 35 5
2 37 3
我想要这样的结果:
============================
Info | class | total
| 1 | 2 |
============================
in 35 37 72
out 5 3 8
那么如何在mysql中进行查询以获得这些结果呢?
请帮助我,我已多次尝试但未成功
由于
答案 0 :(得分:0)
将两个支点与UNION
组合在一起。
SELECT "in" AS Info,
MAX(CASE WHEN class = 1 THEN `in` END) AS class1,
MAX(CASE WHEN class = 2 THEN `in` END) AS class2,
SUM(`in`) AS total
FROM yourTable
UNION
SELECT "out" AS Info,
MAX(CASE WHEN class = 1 THEN `out` END) AS class1,
MAX(CASE WHEN class = 2 THEN `out` END) AS class2,
SUM(`out`) AS total
FROM yourTable