作者:四年夏至_175 | 来源:互联网 | 2023-08-01 06:19
最近做项目,在做搜索模块的时候,模糊查询肯定少不了。
今天就详细的看一下模糊查询:
$where[‘title‘] = array(‘like‘,‘%‘.$words.‘%‘);
$where[‘title‘] = array(‘like‘,array(‘%‘.$words.‘%‘,‘%‘.$wordss.‘%‘));
$where[‘title‘] = array(array(‘like‘,‘%‘.$words.‘%‘),array(‘like‘,‘%‘.$wordss.‘%‘));
一、当个字段做模糊查询。
$words = "吃";
$where[‘title‘] = array(‘like‘,‘%‘.$words.‘%‘);
$list = $TagDB->where($where)->select();
这样执行的Sql是:
SELECT * FROM `tp_keywords_sanqi` WHERE `title` LIKE ‘%吃%‘
这个可以查询多个字段:
$where[‘title‘] = array(‘like‘,‘%‘.$words.‘%‘);
$where[‘description‘] = array(‘like‘,‘%‘.$words.‘%‘);
$list = $TagDB->where($where)->select();
上面的坏处:很多时候,我们做的是标题 title的查询,而且查询的时候,要求包含 title 包含两个或者以上的词:
例如:
要求一:查询标题包含 “作用” 或 “怎样” ;
$words = "怎样";
$wordss = "作用";
$where[‘title‘] = array(‘like‘,array(‘%‘.$words.‘%‘,‘%‘.$wordss.‘%‘));
$list = $TagDB->where($where)->select();
执行的Sql是这样的:
SELECT * FROM `tp_keywords_sanqi` WHERE (`title` LIKE ‘%怎样%‘ OR `title` LIKE ‘%作用%‘)
要求二:查询标题中同时包含 “作用” 和 ”怎样“ ;
$words = "怎样";
$wordss = "作用";
$where[‘title‘] = array(array(‘like‘,‘%‘.$words.‘%‘),array(‘like‘,‘%‘.$wordss.‘%‘));
$list = $TagDB->where($where)->select();
执行的sql是这样的:
SELECT * FROM `tp_keywords_sanqi` WHERE ( `title` LIKE ‘%怎样%‘ AND `title` LIKE ‘%作用%‘ )
当然啦!还可以自己写Sql来执行:
$sql = "SELECT * FROM `tp_keywords_sanqi` WHERE `title` LIKE ‘%什么%‘;";
$sql = "SELECT * FROM `tp_keywords_sanqi` WHERE (`title` LIKE ‘%什么%‘) OR (`title` LIKE ‘%吃%‘);";
$sql = "SELECT * FROM `tp_keywords_sanqi` WHERE (`title` LIKE ‘%什么%‘ AND `title` LIKE ‘%作用%‘)";
$list = M()->query($sql);