我们常常有这样的需求,比如搜索。
搜索出,标题,子标题,内容中包含某某关键字。
这就要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