mysql怎么写复杂sql_mysql 一个复杂的sql

查询 物资明细:

SELECT r.material_id,r.material_category,r.material_name,r.material_quality,

r.standard,r.product_factory,r.material_type,r.measure_unit,(r.num+r.num_sub) as num,

(r.num+r.num_sub)-num_sd-num_yd as current_js_num

FROM

-- 单独计算各个数值

(

SELECT material_base.*

,

-- 合同数量; parent_id

(

SELECT SUM(num) from scm_material_contract

GROUP BY material_id,form_id,audit_status HAVING material_id=material_base.material_id

and form_id='1111' and audit_status='1'

)

AS num,

(

SELECT SUM(num) from scm_material_contract_add

GROUP BY material_id,parent_id,audit_status HAVING material_id=material_base.material_id

and parent_id='1111' and audit_status='1'

)

AS num_sub,

(

SELECT SUM(current_js_num) from scm_material_settle_sd

GROUP BY material_id,parent_id,audit_status HAVING material_id=material_base.material_id

and parent_id='1111' and audit_status='1'

)

AS num_sd,

(

SELECT SUM(current_js_num) from scm_material_settle_yd

GROUP BY material_id,parent_id,audit_status,chongxiao_status HAVING material_id=material_base.material_id

and parent_id='1111' and audit_status='1'

and chongxiao_status !='1'

)

AS num_yd

FROM(

SELECT material_id,material_category,material_name,material_quality,standard,product_factory,material_type,measure_unit FROM scm_material_base WHERE

material_id in(

-- 找出所有的物料编号

SELECT material_id from scm_material_contract where form_id='1111' and audit_status='1'

union

SELECT material_id from scm_material_contract_add ma INNER JOIN scm_purchase_contract_add ac ON ma.form_id=ac.sub_contract_id

where ac.contract_id='1111' and ac.audit_status='1'

)

)as material_base

)as r

b04ee9b76c91066996fc6503c7fdea44.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值