包含以下列的表:
id | name1 | score1 | name2 | score2
我需要在一个
中转换这两个查询的结果SELECT name1, SUM(score1) as a
FROM partidos
GROUP BY name1
ORDER BY a DESC;
SELECT name2, SUM(score2) as b
FROM partidos
GROUP BY name2
ORDER BY b DESC;
结果为“a + b
”,我需要在一个查询中使用此总和和名称。
示例:
id | name1 | score1 | name2 | score2
1 | james | 5 | carolina | 3
2 | carolina | 3 | troll | 9
3 | mordor | 6 | ent | 5
4 | carolina | 1 | paul | 3
5 | paul | 18 | kek | 1
结果:
paul 21
troll 9
carolina 7
mordor 6
ent 5
james 5
kek 1
答案 0 :(得分:1)
使用UNION ALL
将Score1
和Score2
合并为一列,然后执行SUM
select Name, SUM(Total)
from
(
SELECT name1 as Name, score1 as Total
FROM partidos
union all
SELECT name2, score2 as Total
FROM partidos
) A
GROUP BY Name