Thinkphp method of realizing multi-field fuzzy matching query _php example

Source: Internet
Author: User
Tags php template codeigniter zend framework

This paper illustrates the method of thinkphp fuzzy matching query with multiple fields. Share to everyone for your reference, specific as follows:

Foreword: Sometimes the query matches more than one field. such as query address, the address is composed of several fields. There are provinces, cities, districts and so on, as well as detailed address. How do you inquire at this time?

To implement the same query criteria for different fields

$User = M ("User"); Instantiate the User object
$map [' name|title '] = ' thinkphp ';
The query condition is passed into the query method
$User->where ($map)->select ();

To use in a project

if ($address) {
  //Address query
  $where [' b.province|b.city|b.area|b.detail '] = array (' Like ', '% '. $address. ') %');
        $this->assign (' address ', $address);
}

Here it is very simple to solve this demand, and very accurate.

The resulting 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 ' = ' ") and (a.member_id in (' 7 ')) and ((b.province like '% Sucheng District% ") 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 desc LIMIT


As you can see from the SQL statement, the parentheses in the where, the and,or combination is ingenious.

Screenshot below

More interested readers of thinkphp related content can view the website topic: "thinkphp Introductory Course", "thinkphp template Operation Skill Summary", "thinkphp Common Method Summary", "The CodeIgniter Introductory Course", "CI ( CodeIgniter) Framework Advanced tutorials, introductory tutorials for the Zend framework Framework, Smarty Templates Primer tutorial, and PHP template technology summary.

I hope this article will help you with 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.