This article mainly introduces the method of thinkPHP to implement multi-field fuzzy matching query, and analyzes the related model operations and sql statements of thinkPHP for fuzzy matching query in the form of examples. Friends in need can refer to this article
The example describes how thinkPHP implements multi-field fuzzy matching query. Share it with everyone for your reference, the details are as follows:
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?
Achieve 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
Related recommendations:
Framework Thinkphp5 Simple implementation of behavior hook Hook
The above is the detailed content of ThinkPHP method to implement multi-field fuzzy matching query. For more information, please follow other related articles on the PHP Chinese website!