How to implement multi-field fuzzy matching query in PHP

墨辰丷
Release: 2023-03-28 12:20:01
Original
3219 people have browsed it

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();
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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!