关于一个PHP mysql 多条件查询的问题

这是一个多条件查询拼接的语句

if($sex and is_array($service)){

 $values = implode("%' OR service LIKE '%",$service);
$sql.="   and  service like '%".$values."%'  ";
$sql.=" and sex='$sex'";   

}

现在存在一个问题,如果在这种条件下会生成的结果是:
select * from dr_member_data where zsoff='是' and sfph='1' and uid<>'1' and sex='男' and service = '%逛街%' OR service = '%就医陪诊%'limit 0,10

数据库中将生成结果:

clipboard.png
那么最后一条:sex=女 的这条信息的结果是不匹配的 是从$sql.=" and service like '%".$values."%' ";得出的结果,我想排除$sql.=" and service like '%".$values."%' "; sex=女的这条结果,sql语句应该怎么拼接呢

阅读 6.5k
2 个回答

select * from dr_member_data where zsoff='是' and sfph='1' and uid<>'1' and sex='男' and (service = '%逛街%' OR service = '%就医陪诊%') limit 0,10

if($sex and is_array($service)){
    $values = implode("%' OR service LIKE '%",$service);
    $sql.="   and (service like '%".$values."%')";
    $sql.=" and sex='$sex'";   
}

select * from dr_member_data where (zsoff='是' and sfph='1' and uid<>'1' and sex='男' and service = '%逛街%') OR service = '%就医陪诊%'limit 0,10

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题