我们常常有这样的需求,比如搜索。
搜索出,标题,子标题,内容中包含某某关键字。
这就要and,or结合使用了。

$where = ['is_show'=>1,'status'=>1]; // 默认查询条件
// 查询条件
if ($keywords = $_POST['keywords']) {
    $map['title'] = ['like','%'.$keywords.'%'];
    $map['age']  = ['like',$keywords.'%'];
    $map['address']  = ['like','%'.$keywords.'%'];
    $map['_logic'] = 'or';
    $where['_complex'] = $map;
}

巧妙的解决了这个问题,或者你通过字符串拼接也可以。

SELECT `id`,`title`,`title_img`,`age`,`gender`,`address` FROM `tf_student` WHERE `is_show` = 1 AND `status` = 1 AND (  `title` LIKE \'%20%\' OR `age` LIKE \'20%\' OR `address` LIKE \'%20%\' ) ORDER BY id desc LIMIT 0,2

相关文章:

  • 2022-12-23
  • 2019-09-16
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2019-08-31
  • 2020-06-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案