企业建设网站是网络营销吗,dw做网站投票,康桥网站建设,免费php开源建站系统使用thinkPHP怎么实现一个子查询语句发布时间#xff1a;2021-01-30 13:31:08来源#xff1a;亿速云阅读#xff1a;85作者#xff1a;Leah这篇文章给大家介绍使用thinkPHP怎么实现一个子查询语句#xff0c;内容非常详细#xff0c;感兴趣的小伙伴们可以参考借鉴#x…使用thinkPHP怎么实现一个子查询语句发布时间2021-01-30 13:31:08来源亿速云阅读85作者Leah这篇文章给大家介绍使用thinkPHP怎么实现一个子查询语句内容非常详细感兴趣的小伙伴们可以参考借鉴希望对大家能有所帮助。理解好sql语句就能用好thinkphp等框架中的数据库操作原sqlSELECT a.*,b.* from (SELECT a.id as opener_id,a.name,sum(c.money) as bonus_money,c.year,c.month from sh_opener aLEFT JOIN sh_opener_bonus b on a.id b.opener_idLEFT JOIN sh_incentive c on b.incentive_id c.idwhere a.agent_id 3 and a.status 1 and c.year 2015 and c.month 11GROUP BY a.id,c.year,c.month) aLEFT JOIN (SELECT a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number from sh_opener_bonus_payment awhere a.year 2015 and a.month 11 and a.agent_id 3) bon a.opener_id b.opener_id;这里面有两个子查询语句其实子查询语句也是表只不过是存在内存中罢了。thinkphp实现$useYear date(Y,strtotime(last month));$this-assign(useYear,$useYear);$useMonth date(m,strtotime(last month));$this-assign(useMonth,$useMonth);// 获取上一月人员的奖金金额// 子查询1$whereSub1[a.agent_id] $this-agent_id;$whereSub1[a.status] 1;$whereSub1[c.year] $useYear;$whereSub1[c.month] $useMonth;$subQuery1 M()-table(sh_opener a)-join(sh_opener_bonus b on a.id b.opener_id)-join(sh_incentive c on b.incentive_id c.id)-where($whereSub1)-group(a.id,c.year,c.month)-field(a.id,a.name,sum(c.money) as bonus_money,c.year,c.month)-select(false);// 子查询2$whereSub2[a.agent_id] $this-agent_id;$whereSub2[a.year] $useYear;$whereSub2[a.month] $useMonth;$subQuery2 M()-table(sh_opener_bonus_payment a)-where($whereSub2)-field(a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number)-select(false);$list M()-table($subQuery1. a)-join($subQuery2. b on a.id b.opener_id)-select();$this-assign(list,$list);关于使用thinkPHP怎么实现一个子查询语句就分享到这里了希望以上内容可以对大家有一定的帮助可以学到更多知识。如果觉得文章不错可以把它分享出去让更多的人看到。