thinkPHP 1个字段匹配多个like查询

潇潇六月雨

thinkphp5 一个字段对应多个模糊查询


$condition['kname'] = array(array('like','%保健%'), array('like','%护膝%'), array('like','%保健护膝护腰护颈%'), 'or'); 

如果是动态的多个条件可以如下:

$condition['question'] = array('or');//question为字段名

foreach ($keyword as $value) {

array_unshift( $condition['question'], array('like', '%'.$value.'%'));

}

使用原生拼接,则如下,显然没有上述方法好

$sql = 'select count(*) from '.$item['table_name'].' where ';

foreach ($keyword as $value) {

$sql .= ' `question` like '.'"%'.$value.'%"'.' or';

}

$sql = substr($sql, 0, -2);

$sql.= ' and is_delete=0';

$item_total = Db::query($sql);

$data[$key]['total'] = $item_total[0]['count(*)'];

你可能感兴趣的:(thinkPHP 1个字段匹配多个like查询)