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();
  • 相关阅读:
    def __unicode__(self): 或 def __str__(self):
    通过ORM创建数据库链接
    单表查询的API介绍
    数据库的查看以及建立数据库
    基础数据类型
    表单及数据库
    flag标志位
    Console面板小技巧:
    angular入门试水-基本指令理解
    构造方法
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/5019383.html
Copyright © 2011-2022 走看看