2017-03-06 51 views
1

我有ff。結構:在兩個colums中發生的MySQL數量

id|player1|player2|win |lose |leave|status 
1 |151663 |150000 |151663|150000|0 |1 
2 |150000 |151663 |150000|151663|0 |1 
3 |151663 |150000 |151663|150000|0 |1 

我需要像導致

player|win|lose|leave 
150000|1 |2 |0 
151663|2 |1 |0 

謝謝! :)

回答

2

您可以使用union all和聚集:

select player, sum(win) as wins, sum(lose) as losses, 
     sum(leave) as leaves 
from ((select win as player, 1 as win, 0 as lose, 0 as leave 
     from ff 
     where win <> 0 
    ) union all 
     (select lose as player, 0 as win, 1 as lose, 0 as leave 
     from ff 
     where lose <> 0 
    ) union all 
     (select leave as player, 0 as win, 0 as lose, 1 as leave 
     from ff 
     where leave <> 0 
    ) 
    ) wll 
group by player; 
+0

完美的人.. !! –

0
SELECT Player,sum(win)win,sum(lose)lose,leave FROM(
SELECT win Player,1 as win,0 as lose,leave FROM @Table 
UNION ALL 
SELECT lose Player,0 as win,1 as lose,leave FROM @Table 
)A group by Player,leave 
相關問題