thinkphp实现多个子查询语句
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语句。
thinkphp实现多个子查询语句的更多相关文章
- ThinkPHP(3)SQL查询语句
ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...
- sql语句编写 有时候一个子查询可以拆分成多个子查询
sql语句编写 有时候一个子查询可以拆分成多个子查询
- thinkphp中的查询语句
<?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...
- ThinkPHP - 查询语句
public function index(){ // + ----------------------- // | 查询语句 // + ----------------------- // 实例化模 ...
- 【三十三】thinkphp之SQL查询语句(全)
一:字符串条件查询 //直接实例化Model $user=M('user1'); var_dump($user->where ('id=1 OR age=55')->select()); ...
- ThinkPHP 数据库操作(三) : 查询方法、查询语法、链式操作
查询方法 条件查询方法 where 方法 可以使用 where 方法进行 AND 条件查询: Db::table('think_user') ->where('name','like','%th ...
- mysql查询语句select-子查询
1 子查询定义 在一个表表达中可以调用另一个表表达式,这个被调用的表表达式叫做子查询(subquery),我么也称作子选择(subselect)或内嵌选择(inner select).子查询的结果传递 ...
- (转)经典SQL查询语句大全
(转)经典SQL查询语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql s ...
- 经典SQL查询语句大全
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
随机推荐
- thinkphp3.2.3 定时任务重新加载, 无法加载新的定时任务的问题
thinkphp3.2.3 的定时任务有个坑,一旦你改名定时任何或者路径,新的定时任务将无法加载,无论你重启php还是重启nginx,甚至重启服务器,都不行. 原因是你要删掉一个类似lock文件,才可 ...
- LeetCode第[49]题(Java):Group Anagrams
题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...
- node 模块部分介绍
chai 断言框架 mocha mochawesome 对mocha 定制报告,生成完整成熟的报告. node-fetch 服务器版fetch superagent 是node 客户端请求代理 ...
- ImageView显示网络上的图片
ImageView显示网络上的图片 一.简介 二.方法 1)ImageView显示网络上的图片方法 第一步:从网络上下载图片 byte[] byteArr = downImage();//这个是自己写 ...
- 图片与路径(Path)的应用
图片的应用:软盘样式的保存按钮,笔记本样式的编辑按钮:只能用图片 路径(Path)的应用:异形轮廓(各种气泡框,普通控件无法描述):异形线条(普通控件无法描述):图片(不建议,因为展现效果不好,比如: ...
- Windows下下载及安装numpy、pandas及简单应用
下载numpy 下载地址 https://pypi.python.org/pypi/numpy 进入网站,下载和自己电脑及电脑中安装的python匹配的numpy版本.我的电脑是Win 10 x64位 ...
- 51nod 1043 数位dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 1043 幸运号码 基准时间限制:1 秒 空间限制:131072 ...
- Java堆、栈和常量池以及相关String的详细讲解(转)
一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- Cannot run program "svn" (in directory "G:\IntelliJ IDEA 2017.2.5"): CreateProcess error=2, 系统找不到指定的文件。
- Android 静默安装/后台安装& Root permission
Android 静默安装/后台安装& Root permission 静默安装其实很简单,今天在网上找资料找半天都说的很复杂,什么需要系统安装权限.调用系统隐藏的api.需要系统环境下编译.需 ...