thinkPHP method to implement multi-field fuzzy matching query_php example

WBOY
Release: 2023-03-03 06:34:02
Original
1082 people have browsed it

The example in this article describes how thinkPHP implements multi-field fuzzy matching query. Share it with everyone for your reference, the details are as follows:

Introduction: Sometimes queries need 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 inquire 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

Used 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

It can be seen from the sql statement that the brackets, AND, and OR in where are combined very cleverly.

Screenshots are as follows

Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "Introduction to ThinkPHP Tutorial", "Summary of thinkPHP Template Operation Skills", "Summary of Common Methods of ThinkPHP", "Introduction to Codeigniter Tutorial", "CI (CodeIgniter) Framework" Advanced Tutorial", "Zend FrameWork Framework Introductory Tutorial", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".

I hope this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.

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