mysql unionall 性能_多条链接查询sql的优化 union all 语句 如何优化提高性能

select * from(

select (select dep_name from department where dep_id= o.dep_id) depName,

out_remark ,sum(out_total_amount) total,out_date   From out_warehouse o where

dep_id in (select  dep_id from department)

Group By dep_id Having Count(*) > 1

ORDER BY dep_id

) t1

union all

select * from

( select (select dep_name from department where dep_id = o.dep_id) depName,

out_remark,sum(out_total_amount) total,out_date  From  out_warehouse o  Group By dep_id Having Count(*) = 1

ORDER BY dep_id

) t2

union all

select * from (

select  (select dep_name from department where dep_id = o1.dep_id) depName,out_remark,out_total_amount,out_date from out_warehouse o1 where  dep_id in

( select dep_id From out_warehouse o where

dep_id in (select  dep_id from department)

Group By dep_id Having Count(*) > 1

ORDER BY dep_id

) order by dep_id

) p1

union all

select * from (

select  (select dep_name from department where dep_id = o1.dep_id) depName,out_remark,out_total_amount,out_date from out_warehouse o1 where  dep_id not in

( select dep_id From out_warehouse o where

dep_id in (select  dep_id from department)

Group By dep_id Having Count(*) > 1

ORDER BY dep_id

) order by dep_id

) p2

查询结果

depName out_remark    total out_date

维修科  9号楼修漏 16.84 2015/8/28

供水科 xxx号井用 11.2 2015/8/26

门市部 马步村修漏用5.6         2015/8/28

维修科 9号楼修漏 3.08 2015/8/10

维修科 8号楼修漏 8.16 2015/8/16

维修科 10号楼修漏 5.6     2015/8/21

供水科 xxx号井用 5.6    2015/8/26

供水科 1号楼修漏 5.6    2015/8/28

门市部 马步村修漏用5.6    2015/8/29

需要实现效果是:

查询出总的金额 后面是 详细金额数据

-1445609882795990597.jpg

求教sql语句 如何实现 和 优化 ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值