首页 > 代码库 > MySQL 基础查询

MySQL 基础查询

6月业绩榜

名次 伙伴业绩签单面谈每日目标
1杜艳花1236720查看目标
2郑东杰234510查看目标
3黄传龙34511查看目标
4测试000查看目标
5李夏000查看目标
6王恪001查看目标
7陈爽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(面谈记录表)的运营人员的当月面谈次数总和。