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的同时,把每个条件下的数量都统计出来了。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM