6月业绩榜
名次 伙伴业绩签单面谈每日目标
1MySQL 基础查询-LMLPHP杜艳花1236720
2MySQL 基础查询-LMLPHP郑东杰234510
3MySQL 基础查询-LMLPHP黄传龙34511
4MySQL 基础查询-LMLPHP測试000
5MySQL 基础查询-LMLPHP李夏000
6MySQL 基础查询-LMLPHP王恪001
7MySQL 基础查询-LMLPHP陈爽000

当我拿到以上业务需求时,我就马上在想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(面谈记录表)的运营人员的当月面谈次数总和。

05-25 21:51