Home > Backend Development > PHP Tutorial > ThinkPHP method to implement multi-field fuzzy matching query

ThinkPHP method to implement multi-field fuzzy matching query

不言
Release: 2023-03-25 10:10:01
Original
2556 people have browsed it

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();
Copy after login

Use it in the project

if ($address) {
  // 地址查询
  $where['b.province|b.city|b.area|b.detail'] = array('like', '%'.$address.'%');
        $this->assign('address', $address);
}
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template