0

I have a column name in my table. Which have value Fresh Cream for example.

And input string have value like This product is made of fresh cream and it is fresh made.

I have tried using regular expression by splitting the string on base of string like

    Select * From products where product_name REGEXP ('[[:<:]]This[[:>:]]|[[:<:]]product[[:>:]]|[[:<:]]made[[:>:]]|[[:<:]]of[[:>:]]|[[:<:]]fresh[[:>:]]|[[:<:]]cream[[:>:]]')

But in this case it is also getting the records with the value Chicken with cream. But I want exact match of Fresh cream.

Is there a way to use locate() function like that,

    Select * from products where locate(poducts.product_name , 'This product is made of fresh cream and it is fresh made')>0

Or something like That

4
  • Can't you use LIKE for this? Commented Sep 11, 2017 at 9:44
  • 1
    "Select * from products where product_name like '%fresh cream%" Commented Sep 11, 2017 at 9:44
  • But i don't have exact word i have a string in which multiple names could be there Commented Sep 11, 2017 at 9:45
  • CONCAT a % before and after your product_name column value, and then use LIKE to compare with the static search input string. Commented Sep 11, 2017 at 9:46

2 Answers 2

2

You need to get all the 'name' then check if your input contain one of those. Then you can use "LIKE % %" expression

list_of_name = SELECT product_name FROM products;
input = "This product is made of fresh cream and it is fresh made.";
foreach (list_of_name as name){
    if (strpos(input, name) !== false){
        matched_name = name;
        break;
    }
expr = "%" . matched_name . "%";
product = SELECT * FROM products WHERE product_name LIKE expr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if product table have a large number of data? Will it cause speed or any other issue in this case?
I don't think so, the only data we iterate here is "name" which are strings, I don't think that you have more than 100.000 names in your tables?, it take less then 1s to iterate through a few hundred thousands strings so..
0

I think you are over thinking this. Or I'm missing something. But shouldn't this work (and be a lot faster than REGEXP):

WHERE product_name LIKE '%fresh cream%'

That will get all records that contain fresh cream exactly and any preceding or following string.

6 Comments

Any source for the speed claim?
@Script47 REGEXP can never use an index while LIKE can.
Thanks for reply. But i have long string value. How could i get this name from this string. As i don't even know this name exist in the input or not
@Kaleem, in that case. LOCATE() is one option or you can move some logic to your scripting language and pick out the right word from the sentence before using it in a query.
@Kaleem, there is no fast way of doing what you want to do in MySQL alone. I suggest you get all the product_name first then pick it out from the sentence.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.