数据库联合查询输出数量
程序员文章站
2022-06-24 09:00:36
用tp方法查询班级表,同时在表内输出学生人数 $db_clas = M('clas'); $db_stu = M('stu'); $data_clas = $db_clas->select(); //foreach 循环 foreach($data_clas as $item=>$value){ $ ......
用tp方法查询班级表,同时在表内输出学生人数
$db_clas = M('clas');
$db_stu = M('stu');
$data_clas = $db_clas->select();
//foreach 循环
foreach($data_clas as $item=>$value){
$count = $db_stu->where('clas_id='.$value['id'])->count();
$data_clas[$item]['count']=$count; //在查询的结果中添加字符段
}
//for 循环
$count =$db_clas->count();
for ($i=0;$i<$count;$i++){
$data[$i]['count']=$db_stu->where(array('clas_id'=>$data[$i]['id']))->count();
}
上一篇: 编程范式--对象范式,这不是原来的配方了
下一篇: html5新特性