子查询语句的thinkphp实现

简介:

语句

SELECT a.id as item_id,a.name as item_name,a.intro as item_intro,b.id,b.money FROM sh_incentive_item a left join ( SELECT * FROM `sh_incentive` WHERE ( `agent_id` = 3 ) AND ( `year` = 2016 ) AND ( `month` = 1 ) ) b on a.id = b.item_id;

 

如何实现

SELECT * FROM `sh_incentive` WHERE ( `agent_id` = 3 ) AND ( `year` = 2016 ) AND ( `month` = 1 );

 

复制代码
$incentiveModel = M('Incentive');
        $useYear = date('Y',strtotime('next month'));
        $useMonth = date('m',strtotime('next month'));
        $where['agent_id'] = $this->agent_id;
        $where['year'] = $useYear;
        $where['month'] = $useMonth;
        // 子查询
        $subQuery = $incentiveModel->where($where)->select(false);
复制代码

 

整体

复制代码
   // 获取当前代理商 本年 本月的设置
        $incentiveItemModel = M('Incentive_item');
        $incentiveModel = M('Incentive');
        $useYear = date('Y',strtotime('next month'));
        $useMonth = date('m',strtotime('next month'));
        $where['agent_id'] = $this->agent_id;
        $where['year'] = $useYear;
        $where['month'] = $useMonth;
        // 子查询
        $subQuery = $incentiveModel->where($where)->select(false);
        $list = $incentiveItemModel->table('sh_incentive_item a')->join('left join '.$subQuery.' b on a.id = b.item_id')->field('a.id as item_id,a.name as item_name,a.intro as item_intro,b.id,b.money')->select();





相关文章
原生php实现列表接口+分页接口+排序接口组合使用+包括测试数据(不加任何封装)
原生php实现列表接口+分页接口+排序接口组合使用+包括测试数据(不加任何封装)
原生php实现列表接口+分页接口+排序接口组合使用+包括测试数据(不加任何封装)
|
6月前
|
SQL PHP
ThinkPHP的功能类之联表查询
ThinkPHP的功能类之联表查询
|
6月前
Thinkphp5实现接口嵌套
Thinkphp5实现接口嵌套
|
9月前
|
JSON 安全 前端开发
【Django学习】(四)子应用改写函数视图为类视图
【Django学习】(四)子应用改写函数视图为类视图
|
9月前
|
PHP
fastadmin框架select下拉框该怎么写(接口数据)
fastadmin框架select下拉框该怎么写(接口数据)
281 0
|
SQL PHP
【ThinkPHP】like同一个字段多个条件查询
【ThinkPHP】like同一个字段多个条件查询
513 0
【ThinkPHP】like同一个字段多个条件查询
|
SQL PHP
Thinkphp查询is null数据
Thinkphp查询is null数据
344 0
|
JavaScript 前端开发 PHP
ThinkPHP多表联合查询以及自带分页类用法
ThinkPHP多表联合查询以及自带分页类用法
254 0