名次 | 伙伴 | 业绩 | 签单 | 面谈 | 每日目标 | |
---|---|---|---|---|---|---|
1 | 杜艳花 | 12367 | 2 | 0 | 查看目标 | |
2 | 郑东杰 | 2345 | 1 | 0 | 查看目标 | |
3 | 黄传龙 | 345 | 1 | 1 | 查看目标 | |
4 | 测试 | 0 | 0 | 0 | 查看目标 | |
5 | 李夏 | 0 | 0 | 0 | 查看目标 | |
6 | 王恪 | 0 | 0 | 1 | 查看目标 | |
7 | 陈爽 | 0 | 0 | 0 | 查看目标 |
当我拿到以上业务需求时,我就立即在想SQL语句该怎样写。
试了简单的表左右链接查询left join right join,显然问题没有这么简单,数据查询有误,后来得到大鸟指点,记录下。
select a.yyry_xiaotxiang ,a.yyry_id,a.yyry_xming,c.cc,d.dd ,b.bb
from yyry a
left join (select count(mtjl_id) bb,mtjl_yyryid
from mtjl
where year(now())=year(mtjl_yysjian) and month(now())=month(mtjl_yysjian)
group by mtjl_yyryid)b on a.yyry_id=b.mtjl_yyryid
left join(select xiaoshouid,sum(khsh_yji) cc from khshdjb
where year(now())=year(khsh_jrusjian) and month(now())=month(khsh_jrusjian) group by
xiaoshouid )c
on a.yyry_id=c.xiaoshouid
left join(select count(khsh_id) dd,xiaoshouid
from khshdjb
where year(now())=year(khsh_jrusjian) and month(now())=month(khsh_jrusjian)
group by xiaoshouid) d on a.yyry_id=d.xiaoshouid where a.yyry_jsemcheng = ‘转让‘ and a.yyry_id > 0 order by c.cc desc
这是三表联查,分别查询了yyry(运营人员表) 的图像,ID,姓名,khshdjb(客户售后登记表)的运营人员的当月业绩总和,签单数量,mtjl(面谈记录表)的运营人员的当月面谈次数总和。