This article mainly introduces thinkPHP's method of fuzzy match query with multiple fields, and analyzes the model operations and SQL statements related to fuzzy match query using thinkPHP in the form of examples, for more information about how to use thinkPHP to implement multi-field fuzzy match, see the example in this article. We will share this with you for your reference. The details are as follows:
Introduction: sometimes the query must match multiple fields. For example, the query address is composed of multiple fields. There are provinces, cities, districts, and so on, as well as detailed addresses. How can I query the data at this time?
Implement the same query conditions for different fields
$ User = M ("User"); // instantiate the User object $ map ['name | title'] = 'thinkphp '; // input the query condition into the query method $ User-> where ($ map)-> select ();
Projects used
If ($ address) {// address query $ where ['B. province | B. city | B. area | B. detail '] = array ('like',' % '. $ address. '%'); $ this-> assign ('address', $ address );}
This is a simple and accurate solution.
The generated SQL statement is as follows:
SELECT. *, B .name, B .tel, B .province, B .city, B .area, B .detail, B .zip codeFROM sh_order aLEFT JOIN sh_member_address B on. member_id = B. member_id and B. selected = 1 WHERE ('store _ id' = '10') AND (. member_id IN ('7') AND (B. province LIKE '% Sucheng %') OR (B. city LIKE '% Sucheng District %') OR (B. area LIKE '% Sucheng District %') OR (B. detail LIKE '% Sucheng District %') order by addtime desc, sendtime asc, paytime descLIMIT
From the SQL statement, we can see that the parentheses, AND, OR combinations in the where clause are clever.
As follows: