Kaleem
Kaleem

Reputation: 201

Mysql, How do i query to get name from database on base of string as input?

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

Upvotes: 0

Views: 197

Answers (2)

NewBieBR
NewBieBR

Reputation: 344

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;
}

Upvotes: 2

majidarif
majidarif

Reputation: 20015

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.

Upvotes: 0

Related Questions