sql查询数组中的条件查询

2025-05-18 18:52:10
推荐回答(1个)
回答(1):

将数组分隔, and or 查询

这个是我之前写的一个数组查询的,你可以看下

$where="";

$jd_name=$_POST['jdname'];

if($jd_name){

$where=$where." and (title like '%".$jd_name."%')";

}

$jgqj=$_POST['jgqj'];

if($jgqj){

$str = $jgqj;

$arr = explode(",",$str);

$len=count($arr);

if($len=="1"){

foreach($arr as $u){

$strarr = explode("_",$u);

$tj=" price between ".$strarr[0]." and ".$strarr[1]." ";

}
}else{
foreach($arr as $u){
$strarr = explode("_",$u);
$tj=$tj." price between ".$strarr[0]." and ".$strarr[1]." or ";
}

}

$ntj=rtrim($tj, "or ");

$where=$where." and (".$ntj.")";

}

$pj=$_POST['pj'];

if($pj){

$str1=$pj;

$arr1=explode(",",$str1);

foreach ($arr1 as $k){

$xj=$xj." xingji=".$k." or ";

}
$nxj=rtrim($xj, "or ");

$where=$where." and (".$nxj.")";

}

$jdtype=$_POST['jdtype'];

if($jdtype){

$str2=$jdtype;

$arr2=explode(",",$str2);

foreach ($arr2 as $ke){

$type=$type." jdtype='".$ke."' or ";

}

$ntype=rtrim($type, "or ");

$where=$where." and (".$ntype.")";

}

$ss=$_POST['ss'];

if($ss){

$str3=$ss;

$arr3=explode(",",$str3);

foreach ($arr3 as $key){

$sheshi=$sheshi." sheshi like '%".$key."%' or ";

}

$nsheshi=rtrim($sheshi, "or ");

$where=$where." and (".$nsheshi.")";

}

$pf=$_POST['pf'];

if($pf){

$where=$where.$pf;

}

$ziduan=$_POST['ziduan'];

if($ziduan){

if($ziduan=="jg"){

$order=" order by price";

}elseif($ziduan=="xj"){

$order=" order by xingji";

}elseif($ziduan=="dp"){

$order=" order by id";

}

}
$shunxu=$_POST['shunxu'];

if($shunxu){

if($shunxu=="1"){

$order=$order." asc";

}else{

$order=$order." desc";

}

}

$Text="";

$sql1=$empire->query("select classid from {$dbtbpre}enewsclass where classname='$city'");