1.连表查询
2先查出副表的id,再在主表查询需要的字段,这样在循环查询数据库,不建议这样做
3数组键值对比(大大减轻数据库压力)
$data=$demandModel->field('medicine_id,sum(number) as number')->where($where)->group('medicine_id')
->order('add_time desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$name=M('medicine')->field('id,name')->select();//整个基药库
//处理成键值对,数组键值对应成数据表id
$name_list=i_array_column($name, $name['name'],'id');
$data=i_array_column($data, $data['medicine_id'],'medicine_id');//满足条件的药品id
//获取药名
foreach($data as $k=>$v){
if(in_array($v['medicine_id'], $name_list[$k])){
$data[$k]['name']=$name_list[$k]['name'];
}
}