在这里举例:
客户-》项目 一对多
项目-》合同 一对多
khdw 客户表 id khmc
xmxx 项目表 id khbh
htxx 合同表 id xmbh htje
1.查询某个客户的合同额(很简单,直接根据客户编号关联查询)
select sum(htje) as htje from htxx where qdsj>= '20180101' and qdsj<= '20201231' and xmbh in
(select id from crm_xmxx where khbh = '指定客户的ID')
2.查询所有客户的合同额,分组显示:(思路:先按照三级合同表左连接查询项目信息和客户信息,然后使用聚合函数查询)
select sum(htje) as htje,khbh from (
select a.htje,b.khbh,c.khmc from htxx a left join xmxx b on a.xmbh = b.id left join khdw c on b.khbh= c.id)
group by khbh