数据库联合查询输出数量
程序员文章站
2022-05-16 21:11:04
用tp方法查询班级表,同时在表内输出学生人数
$db_clas = M('clas');
$db_stu = M('stu');
$data_...
用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一步一步走——多媒体对象(三)