sql之合理使用sum case
2019-03-22 本文已影响0人
ssttIsme
SELECT TIME 时间,
(SELECT COUNT(result) FROM game g WHERE result='胜' AND g.time=game.`time`) 胜,
(SELECT COUNT(result) FROM game g WHERE result='负' AND g.time=game.`time`) 负
FROM game GROUP BY TIME;
SELECT TIME 时间,
SUM(CASE WHEN result='胜' THEN 1 ELSE 0 END) 胜,
SUM(CASE WHEN result='负' THEN 1 ELSE 0 END) 负
FROM game GROUP BY TIME;