This article mainly introduces the method of thinkphp to realize multi-field fuzzy matching query, and analyzes the related model operation and SQL statement of thinkphp for fuzzy matching query with the example form, and the friend can refer to the following
This paper describes the method of thinkphp to implement multi-field fuzzy matching query. Share to everyone for your reference, as follows:
Introduction: Sometimes queries are matched to multiple fields. For example, a query address, the address is composed of multiple fields. There are provinces, municipalities, districts and so on, as well as detailed addresses. How do you check this time?
Implement the same query criteria for different fields
$User = M ("User"); Instantiate the User object $map[' name|title ' = ' thinkphp ';//pass query condition into Query method $user->where ($map)->select ();
Used in the project
if ($address) { //Address query $where [' b.province|b.city|b.area|b.detail '] = array (' Like ', '% '. $address. ' %'); $this->assign (' address ', $address);}
This is a simple solution to this requirement, and it is very precise.
The resulting SQL statement is as follows
SELECT a.*,b.name,b.tel,b.province,b.city,b.area,b.detail,b.zipcodefrom sh_order aleft JOIN sh_member_address B on a.member_id = b.member_id and b.selected = 1WHERE (' store_id ' = ' ten ') and (a.member_id in (' 7 ')) and ((B.province LIK E '% Sucheng District ') or (b.city like '% of Sucheng District% ') or (B.area like '% Sucheng District% ') or (B.detail like '% Sucheng District% ') "ORDER by addtime Desc, sendtime A SC, paytime desclimit 0,10
As you can see from the SQL statement, the parentheses in the where, the and,or combination is ingenious.
As follows