sql语句博大精深

理解好sql语句,就能用好thinkphp等框架中的数据库操作

原sql

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 a
LEFT JOIN sh_opener_bonus b on a.id = b.opener_id
LEFT JOIN sh_incentive c on b.incentive_id = c.id
where a.agent_id = and a.status = and c.year = and c.month =
GROUP BY a.id,c.year,c.month) a
LEFT JOIN (SELECT a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number from sh_opener_bonus_payment a
where a.year = and a.`month` = and a.agent_id = ) b
on 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'] = ;
$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框架对sql的封装,最终还是要拼凑成sql语句。

最新文章

  1. Android监听系统短信数据库变化-提取短信内容
  2. C# 以管理员身份运行WinForm程序
  3. jdk安装配置具体分析
  4. Foundation和UIKit框架组织图
  5. taglib指令
  6. 5.Primitive, Reference, and Value Types
  7. java内部类的继承
  8. Spring Auto proxy creator example
  9. 汉诺塔 python版
  10. 解决网站出现GET .woff 404 (Not Found)的问题
  11. iOS利用通知(NSNotification)进行传值
  12. 最简单的html5语言
  13. Golang使用pprof和qcachegrind进行性能监控
  14. apply函数用法
  15. Ubuntu12.04中的虚拟机安装Ubuntu16.04,并实现远程控制16.04
  16. windows 动态库的封装以及调用
  17. python nose测试框架全面介绍十一---用例的发现
  18. JavaScript基础知识(数组的方法)
  19. jquery或者JavaScript调用WCF服务的方法
  20. Verilog 加法器和减法器(2)

热门文章

  1. mysql 转移数据目录
  2. php将科学计算法得出的结果转换成原始数据
  3. 卡尔曼滤波——基本假设(1)线性系统(2)高斯分布 根据x(t) 求解x(t+1)
  4. 使用swagger作为restful api的doc文档生成——从源码中去提取restful URL接口描述文档
  5. 【scala】定义变量和函数
  6. Struts10---拦截器
  7. 条款24:如果所有的参数都需要类型转换,那么请为此采用non-member函数
  8. dataGridView的使用经验
  9. VBox自动启动websrv 并开机启动所有的虚拟机
  10. ubuntu 设置plank开机自启之后关机键失效变为注销键