1.FIND_IN_SET的应用:在实际应用中,可能会出现这种情况:查找某个字段String中是否存储了一个id,找出一个列表。例如:找出所有包含6的记录,可以这样查询(这里使用hinkphp):exampleif(!empty($arr)){//遍历subid并处理$length=count($arr);$str="";for($x=0;$x<=$length-1;$x++){$arr2[$x]=$arr[$x]['subid'];//拼接sql语句$str.="orFIND_IN_SET(".$arr2[$x].",first_subject)";//截掉前面的空格andor$res_str=substr($str,4);}$条件[‘检查’]=3;$condition['_string']=$res_str;$count=M("teacher")->where($condition)->count();$Page=new\Think\Page($count,1);$show=$Page->show();$teacher=M("teacher")->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();主要用到的是FIND_IN_SET(a,b),其中a是字符串,b是字段。另一个例子:if(!empty(I("request.indentity"))){$condition['indentity']=I("request.indentity");}if(!empty(I("request.gender"))){$condition['gender']=I("request.gender");}if(!empty(I("request.school"))){$condition['school']=I("request.school");}if(!empty(I("request.subid"))){$condition['_string']="FIND_IN_SET(".I("request.subid").",first_subject)";}if(!empty(I("request.teach_place"))){$condition['_string']="FIND_IN_SET(".I("request.teach_place").",teach_place)";}if((!empty(I("request.teach_place")))&&(!empty(I("request.subid")))){$condition['_string']="FIND_IN_SET(".I("request.subid").",first_subject)和FIND_IN_SET(".I("request.teach_place").",teach_place)";}$条件[‘检查’]=3;2.利用字符串字段查找每个字符对应的相关性表数据的应用:某个字符串由多个id组成,如图,现在我要遍历记录对应的字符串中包含的id相关的表。示例:本示例为二级遍历,用于显示教师列表中可任教的科目列表。遍历数组,逐一取出//遍历选课数组,需要二级组合数组,需要遍历两次$arr_length=count($teacher);for($y=0;$y<=$arr_length-1;$y++){$subject_str=substr($teacher[$y]['first_subject'],0,-1);//字符串转换成subid数组的数组$subject_arr1=explode(",",$subject_str);$length=count($subject_arr1);//for循环使用subid查找课程,然后查找课程名称,然后将subid和课程名称组成一个二维数组for($x=0;$x<=$length-1;$x++){//查找课程名称,一行记录的数组$subject_arr2[$x]=M("subject")->where(array("subid"=>$subject_arr1[$x]))->find();//课程名称数组$subject_arr3[$y][$x]['subid']=$subject_arr2[$x]['subid'];$subject_arr3[$y][$x]['subject']=$subject_arr2[$x]['subject'];}}
