ThinkPHP implements Multi-field fuzzy match query, thinkphp Field

Source: Internet
Author: User

ThinkPHP implements Multi-field fuzzy match query, thinkphp Field

The example in this article describes how thinkPHP implements Multi-field fuzzy match query. We will share this with you for your reference. The details are as follows:

Introduction: Sometimes the query must match multiple fields. For example, the query address is composed of multiple fields. There are provinces, cities, districts, and so on, as well as detailed addresses. How can I query the data at this time?

Implement the same query conditions for different fields

$ User = M ("User"); // instantiate the User object $ map ['name | title'] = 'thinkphp '; // input the query condition into the query method $ User-> where ($ map)-> select ();

Projects used

If ($ address) {// address query $ where ['B. province | B. city | B. area | B. detail '] = array ('like',' % '. $ address. '%'); $ this-> assign ('address', $ address );}

This is a simple and accurate solution.

The generated SQL statement is as follows:

SELECT. *, B .name, B .tel, B .province, B .city, B .area, B .detail, B .zip codeFROM sh_order aLEFT JOIN sh_member_address B on. member_id = B. member_id and B. selected = 1 WHERE ('store _ id' = '10') AND (. member_id IN ('7') AND (B. province LIKE '% sucheng %') 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 descLIMIT

From the SQL statement, we can see that the parentheses, AND, OR combinations in the where clause are clever.

As follows:

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.