数据库记录如下:
现在要查询统计出每个'name'的'result'分别为'success'和'fail'的次数;
利用IF条件判断满足条件为1,不满足为0,再用SUM函数求和,最后通过'name'分组。
SELECT NAME, SUM(IF(result = 'success', 1, 0)) AS success, SUM(IF(result = 'fail', 1, 0)) AS fail FROM if_test GROUP BY `name`
结果如下: