语句

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();
05-20 05:00