thinkphp implementation of multi-field fuzzy matching Query method _php instance

Source: Internet
Author: User
Tags php template codeigniter smarty template zend framework
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

More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "CodeIgniter Getting Started Tutorial", "CI ( CodeIgniter) Framework Advanced Tutorial, Zend Framework Introduction tutorial, Smarty Template Primer Basic Tutorial and PHP template technology summary.

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.