$where['enterprise_id'] = $enterpriseId;
//企业名称不为空
if(!empty($enterpriseName)){$where['enterprise_name'] = array('like',"%$enterpriseName%");}
$table2 = array('enterprise_data.yunkh_enterprise_rel_enterprise'=>'ed_ere','enterprise_data.yunkh_enterprise_info'=>'ed_ei');
$where2 = array('ed_ere.enterprise_id'=>$enterpriseId,'status'=>'1','ed_ei.enterprise_name'=>array('like',"%$enterpriseName%"),'_string'=>'ed_ere.rel_enterprise_id = ed_ei.enterprise_id');
$contractInfoList = $this->model->field('enterprise_id,enterprise_name')->table(array('enterprise_data.yunkh_enterprise_info' => 'ed_id'))->where($where)->union(array('field'=>'ed_ei.enterprise_id,enterprise_name','table'=>$table2,'where'=>$where2),true)->select();return $contractInfoList;执行的sql:
SELECT 'enterprise_id','enterprise_name'
FROM enterprise_data.yunkh_enterprise_info 'ed_id' WHERE 'enterprise_id' = 1 AND 'enterprise_name' LIKE '%农%'
UNION ALL
SELECT ed_ei.enterprise_id,'enterprise_name'
FROM enterprise_data.yunkh_enterprise_rel_enterprise 'ed_ere',enterprise_data.yunkh_enterprise_info 'ed_ei'
WHERE ed_ere.enterprise_id = 1 AND 'status' = '1' AND ed_ei.enterprise_name LIKE '%农%'
AND ( ed_ere.rel_enterprise_id = ed_ei.enterprise_id )
转载于:https://blog.51cto.com/11549783/1772685