php条件查询mysql的两个表妹,关于一个PHPmysql多条件查询的问题

在PHP中,原始的SQL查询语句在多条件组合时产生了错误的结果,导致了性别为'女'的记录被错误地包含。为了解决这个问题,可以调整SQL语句的结构,使用括号来明确条件的优先级。通过修改后的SQL语句,能够正确地排除掉匹配到特定`service`但性别为'女'的记录。
摘要由CSDN通过智能技术生成

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

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

数据库中将生成结果:

bVrIiI

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

回复内容:

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

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

数据库中将生成结果:

bVrIiI

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

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

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值