您好我有一张桌子(Spiel),其中我有两个列(SpielerIDForderer,SpielerIDGefordert)。从这两个col我想要最多出现的记录 并计算在一起。因此,如果值/ id 2在SpielerIDForderer中出现两次,在SpielerIDGefordert中出现七次,则应返回9.
这是我的表
+-------------------+--------------------+
| SpielerIDForderer | SpielerIDGefordert |
+-------------------+--------------------+
| 5 | 2 |
| 3 | 2 |
| 3 | 2 |
| 3 | 8 |
| 6 | 2 |
| 3 | 2 |
| 3 | 2 |
| 2 | 3 |
| 2 | 2 |
+-------------------+--------------------+
这是我的SQL无法正常工作:
SELECT SUM(dum.tab) AS total FROM (
SELECT COUNT(SpielerIDForderer) AS tab FROM pddb.Spiel AS b
UNION ALL
SELECT COUNT(SpielerIDGefordert) AS tab FROM pddb.Spiel AS a WHERE SpielerIDGefordert=SpielerIDForderer
) AS dum
我在这种情况下的预期结果是 9
答案 0 :(得分:5)
试试这个
select id, count(*) as counting from
(
select SpielerIDForderer as id from table
union all
select SpielerIDGefordert from table
) as t group by id
答案 1 :(得分:3)
SELECT value, count(*) AS total FROM (
SELECT SpielerIDForderer AS value FROM pddb.Spiel AS b
UNION ALL
SELECT SpielerIDGefordert AS value FROM pddb.Spiel AS a
) AS dum
group by value
order by total desc
limit 1