Fuzzy and accurate search with php

Source: Internet
Author: User

Fuzzy and accurate are opposite, but today I am not talking about what is a paradox. Let's look at the question below.
The database structure format is as follows:


Id | cid

--------------------------------

1 |, 4, 5, 7,

--------------------------------

3 |, 4, 5, 8,

--------------------------------

4 |, 5, 4,

--------------------------------

6 |, 5, 4, 7, 8,

--------------------------------

7 |, 8, 4, 5, 7, 9,

--------------------------------

8 |,

I 'd like to ask
If the cid values in my post are 4 and 5, how can I precisely query the data with id = 4?
Similarly, if the cid values in my post are 4, 5, 7, and 8, how can I precisely query this data with id = 6?

Note:
The data in the cid column is non-fixed and irregular. The only thing that can be confirmed is that the same value will not appear again.
In addition, I tried to use the like method and link method, which is not accurate enough. for example... where cid like '%, 4, %' and cid like '%, 5, %'

 

The solution is as follows:
He said that he was not as close to himself. He just made a mistake and the result was already coming.
I used three methods to perform the test. During the test, the data in the database is extracted 20000 times in a loop. The results are as follows:

SQL code

// 1770 ms ~ 1800 MS

Select id, type From hotel where type like '% | 21 | %' and type like '% | 101 | %' and LENGTH (type) = length ('| 21 | 101 | ')

SQL code

// 1760 ms ~ 1810 MS

Select id, type From hotel where locate ('| 21 |', type)> 0and locate ('| 101 |', type)> 0and LENGTH (type) = length ('| 21 | 101 | ')

SQL code

// The CPU is directly 100%, and the memory generator fails.

Select id, type From hotel where type REGEXP concat ('. * [', replace ('000000', '|'), ']. *')

 

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.