Home >Backend Development >PHP Tutorial >How to implement multi-field fuzzy matching query in PHP
This article mainly introduces the method of implementing multi-field fuzzy matching query in PHP, and analyzes the related model operations and sql statements of PHP fuzzy matching query in the form of examples. Friends in need can refer to the following
Introduction : Sometimes the query needs to match multiple fields. For example, when querying an address, the address is composed of multiple fields. There are provinces, cities, districts, etc., as well as detailed addresses. How to query at this time?
Realize the same query conditions for different fields
$User = M("User"); // 实例化User对象 $map['name|title'] = 'thinkphp'; // 把查询条件传入查询方法 $User->where($map)->select();
Use it in the project
if ($address) { // 地址查询 $where['b.province|b.city|b.area|b.detail'] = array('like', '%'.$address.'%'); $this->assign('address', $address); }
This requirement is solved very simply and very accurately.
The generated sql statement is as follows
SELECT a.*,b.name,b.tel,b.province,b.city,b.area,b.detail,b.zipcode FROM sh_order a LEFT JOIN sh_member_address b on a.member_id = b.member_id and b.selected = 1 WHERE ( `store_id` = '10' ) AND ( a.member_id IN ('7') ) AND ( (b.province LIKE '%宿城区%') OR (b.city LIKE '%宿城区%') OR (b.area LIKE '%宿城区%') OR (b.detail LIKE '%宿城区%') ) ORDER BY addtime desc, sendtime asc, paytime desc LIMIT 0,10
As can be seen from the sql statement, the brackets, AND, and OR in where are combined very cleverly .
The screenshots are as follows
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implements random acquisition of domain names that are not blocked by WeChat
phpMethods to implement file management and basic functional operations
phpDetailed explanation of login timeout detection function examples
The above is the detailed content of How to implement multi-field fuzzy matching query in PHP. For more information, please follow other related articles on the PHP Chinese website!