zoukankan      html  css  js  c++  java
  • 子查询语句的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();
  • 相关阅读:
    题解[51nod1555] 布丁怪
    题解 [51nod1753] 相似子串
    题解[NOIP2017] 列队
    题解 [NOIP2015]运输计划
    题解 [APIO2014]连珠线
    题解 [ZJOI2010]基站选址
    [学习笔记] kmp
    RPC&ORM
    Spring
    常用工具
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/5019383.html
Copyright © 2011-2022 走看看