Mysql 统计查询

SELECT ub.telphone,
    SUM(IF(b.type = 1 AND b.level = 1, 1, 0)) AS type11,
    SUM(IF(b.type = 1 AND b.level = 2, 1, 0)) AS type12,
    SUM(IF(b.type = 2 AND b.level = 1, 1, 0)) AS type21,
    SUM(IF(b.type = 2 AND b.level = 2, 1, 0)) AS type22,
    SUM(IF(b.type = 2 AND b.level = 3, 1, 0)) AS type23,
    SUM(IF(b.type = 2 AND b.level = 4, 1, 0)) AS type24,
    SUM(IF(b.type = 2 AND b.level = 5, 1, 0)) AS type25,
    SUM(IF(b.type = 2 AND b.level = 6, 1, 0)) AS type26,
    SUM(IF(b.type = 3 AND b.level = 1, 1, 0)) AS type31,
    SUM(IF(b.type = 3 AND b.level = 2, 1, 0)) AS type32,
    SUM(IF(b.type = 3 AND b.level = 3, 1, 0)) AS type33,
    SUM(IF(b.type = 4, 1, 0)) AS type4 
    FROM `tf_user_bag` as ub LEFT JOIN tf_bag as b on ub.bag_id = b.id  GROUP by ub.telphone ORDER BY ub.telphone desc

Group的同时,把每个条件下的数量都统计出来了。

上一篇:python玄学建模(1):线性规划


下一篇:2017年9月17日 JavaScript简介