mysql一条sql查出多个条件不同的sum或count
2017-06-16 19:07
519 查看
最近做java后台遇到很多问题记录一下,有个需求是在一条sql中同时统计到几条条件不一样的sql或count,就用了子查询,希望能帮助到需要的朋友,上代码和效果图:
select
sum(pzgwjscd) as allJsgm,sbjh.nd as nd,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3301') and ptgw.zt != 0 and sbjh.nd=2017 ) as hzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3303') and ptgw.zt != 0 and sbjh.nd=2017) as wzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3304') and ptgw.zt != 0 and sbjh.nd=2017 ) as jxJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3305') and ptgw.zt != 0 and sbjh.nd=2017) as huzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3306') and ptgw.zt != 0 and sbjh.nd=2017 ) as sxJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3307') and ptgw.zt != 0 and sbjh.nd=2017 ) as jhJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3308') and ptgw.zt != 0 and sbjh.nd=2017 ) as qzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3309') and ptgw.zt != 0 and sbjh.nd=2017 ) as zsJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3310') and ptgw.zt != 0 and sbjh.nd=2017 ) as tzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3311') and ptgw.zt != 0 and sbjh.nd=2017 ) as lsJsgm
from fm_jh_ws_ptgw ptgw
left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where 1=1 and ptgw.zt != 0 and sbjh.nd=2017
数据库表
查询结果:
select
sum(pzgwjscd) as allJsgm,sbjh.nd as nd,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3301') and ptgw.zt != 0 and sbjh.nd=2017 ) as hzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3303') and ptgw.zt != 0 and sbjh.nd=2017) as wzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3304') and ptgw.zt != 0 and sbjh.nd=2017 ) as jxJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3305') and ptgw.zt != 0 and sbjh.nd=2017) as huzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3306') and ptgw.zt != 0 and sbjh.nd=2017 ) as sxJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3307') and ptgw.zt != 0 and sbjh.nd=2017 ) as jhJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3308') and ptgw.zt != 0 and sbjh.nd=2017 ) as qzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3309') and ptgw.zt != 0 and sbjh.nd=2017 ) as zsJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3310') and ptgw.zt != 0 and sbjh.nd=2017 ) as tzJsgm,
( select sum(pzgwjscd) from fm_jh_ws_ptgw ptgw left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where substring(ptgw.xzqh,1,4) LIKE CONCAT ('3311') and ptgw.zt != 0 and sbjh.nd=2017 ) as lsJsgm
from fm_jh_ws_ptgw ptgw
left join fm_sb_jh sbjh on ptgw.sbid = sbjh.id
where 1=1 and ptgw.zt != 0 and sbjh.nd=2017
数据库表
查询结果:
相关文章推荐
- php MYSQL 一条语句中COUNT出不同的条件
- MYSQL_一条语句中COUNT出不同的条件
- MYSQL_一条语句中COUNT出不同的条件
- sql利用在一条语句中count出不同的条件的值
- MYSQL中用SUM()代替count(*)做不同条件下的计数运算
- SQL中以count及sum为条件的查询
- SQL中以count及sum为条件的查询---转(http://kiddymeet.51cto.com/blog/20194/19376)
- 在同一个sql语句中如何写不同条件的count数量
- Mysql通过一个限制条件,查出多条不同的记录
- SQL中以count及sum为条件的查询
- Union all的使用实现一条sql把不同表里面的数据查出来,或者按照不同的查询条件查出来的sql累加起来
- COUNT多列,但是每列都是不同条件的,怎么用一句SQL写?
- 在同一个sql语句中写不同条件的count数量,和查询半年的数据
- mysql中一条sql语句中使用多个count关联查询多张表
- SQL中以count及sum为条件的查询 推荐
- php中mysql一条sql语句查询出所有符合条件的数据,该怎么写?
- mysql count(),sum()等作为条件查询
- SQL-基础学习4--聚集函数:AVG(),COUNT(),MAX(),MIN(),SUM();聚集不同值:DISTINCT
- SQL中以count或sum为条件的查询方式
- 如何编写一条SQL实现根据某个字段的条件对某个字段进行不同的update操作