thinkPHP简单实现多个子查询语句的方法
ThinkPHP  /  管理员 发布于 8年前   370
本文实例讲述了thinkPHP简单实现多个子查询语句的方法。分享给大家供大家参考,具体如下: sql语句博大精深 理解好sql语句,就能用好thinkphp等框架中的数据库操作 原sql: 这里面有两个子查询语句,其实子查询语句也是表,只不过是存在内存中罢了。 thinkphp实现: 其实thinkphp框架对sql的封装,最终还是要拼凑成sql语句。 更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》、《smarty模板入门基础教程》及《PHP模板技术总结》。 希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。SELECT 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;
$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);
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号