How to get exact search result using MYSQL LIKE operator - mysql

We have search functionality in our application but problem is that when i searched for term like "pen" it show result from word open in open word there is name pen but we don't want to show such result. we want to show result like xyz pen xyz. we used my sql LIKE in query
So how to achieve this using MY-SQL LIKE?
Thanks in advance..

I assume that you want to find results that contain a word "pen" or any other specified (the title suggests that the whole result should look exactly like the term).
LIKE is too simple to do that, you need to use RLIKE with word boundaries.
[[:<:]] and [[:>:]] are special markers that indicate word start and word end respectively, so instead of:
LIKE '%pen%'
use:
RLIKE '[[:<:]]pen[[:>:]]'
Hope that helps.

Try this
SELECT * FROM table_name WHERE column_name LIKE "% pen %";

If i understand your behavier then you can use:
columnName like '% pen %'

Select * from table where name like 'pen';
"name" is column name for searching.

Related

What is the best way to use MYSQL Search on term not working

What is the best way to search a database for a phrase such as "Almond Anise Cookie" and return the result?
If I
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
and use the phrase "Almond Cookie", nothing is returned as expected. But if I search for "Anise Cookie" the result above is returned.
I've also tried
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
OR name LIKE '".$query."%'
OR name LIKE '%".$query."'
with the same failed result.
Using MATCH AGAINST returns everything that contains "Almond" and everything that contains "Cookie" also not a good result. Is there a happy middle in returned results?
You can try using REPLACE. Something like this should work:
SELECT *
FROM recipes
WHERE NAME LIKE REPLACE(' ".$query." ',' ','%');
Note that I purposely add spaces between .$query. to ensure that the replace operation will make your term filled with the wildcard symbol. In the example above:
If $query='almond cookies' then REPLACE(' ".$query." ',' ','%') will become %almond%cookies%.
You can test the fiddle here : https://www.db-fiddle.com/f/kMzp99S8ENbTkYcW5FVdYN/0

SQL Like query for each word containing keyword

E.g :
I have 1 word for search the data. And the word is 'ayam'.
And this is my query :
select nama_product from product where nama_product like %a%
It works fine. But it show the record that i dont want to be showed.
This is the result :
ayam bakar
daging ayam
bayam hijau
daun bayam
The first and the second result is okay. But the third and the last that i dont want to be showed.
Because i want to show 'ayam' not "b'ayam'"
How do i handle this? I don't know it is duplicate or not. Because i already search it but i didn't find.
If you need to search the word 'ayam' without any prefix and sufix character, you would better use regular expression for that .
e.g.,
SELECT namma_product FROM product WHERE namma_product REGEXP 'ayam'
try this. This will work
Unfortunately this is a limitation of SQL. Some SQL databases have advanced text searching functions such as regular expressions which allow very specific text results.
For your case you will likely have to perform multiple LIKE conditions. For example:
select nama_product
from product
where
nama_product like 'ayam %'
or nama_product like '% ayam %'
or nama_product like '% ayam'
or nama_product = 'ayam'
Please note that if the text fields are very large or if the table is large, queries that rely on LIKE operators can become very slow. LIKE does not scale well with large datasets. If this is a dataset you think will become very large in the future, best to design it in a way where the LIKE operator will not be needed.
You can use regex to match only whole words:
select nama_product
from product
where nama_product regexp '(^| )ayam( |$)';
(^| ) means the word must be the start of the string, or it must be preceded by a space
ayam is the word we're matching
( |$) means the word must be followed by a space, or it must be the end of the string
SQL Fiddle
when u insert '%' before and after it mean All Character before(after) your keyword so just change how u want
for example just keywords start with "ayam":
select nama_product from product where nama_product like 'ayam%'
another example just keywords ended with "ayam":
select nama_product from product where nama_product like '%ayam'
and after that look here ithink your answer is here :
Match only entire words with LIKE?

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

MySQL String Comparison with Wildcards

I wrote a query where a user can input a string and get the data related to that string back from the database.
For example, a user will input Apple even though the full name is Apple Inc.
The code would be laid out as so...
and Description like '%Apple%'
The problem with this is, it will return Snapple along with Apple.
Aside from removing the first "%" wildcard and making the user type more, how can I limit the results to just Apple?
Use a regular expression:
WHERE Description RLIKE '[[:<:]]apple[[:>:]]'
[[:<:]] matches the beginning of a word, [[:>:]] matches the end of a word.
See the documentation for all the regexp operators supported by MySQL
Firstly - string comparison with wild cards (especially leading wild cards) doesn't really scale using "like". You might want to look at full-text searching instead. This basically gives you "google-like" text searching capabilities.
To answer your question, in most cases, "Apple" is a better match than "Snapple" for the term "apple". So, you could include the concept of "match quality" in the search - something like:
select *, 10 as MatchQuality
from table
where description like 'Apple'
union
select *, 5 as MatchQuality
from table
where description like 'Apple%'
union
select *, 1 as MatchQuality
from table
where description like '%Apple%'

specific search in database

I am looking for the method of searching specific words in the mysql with innodb engine. Consider the following example:
Text:
1.Card has two sides.
2.This is my card.
3.We have a car.
4.Car has good color.
5.This was my car.
6.The car is nice.
Suppose that these points are different rows and lie under same column "Text". I need a query that extract the rows with exactly the word "car" using "like", regardless whether the word "car" is at the begining, at the middle or at the end. Keep in mind the "card" is not required.
Use it
SELECT * FROM table WHERE name LIKE '%car' or name LIKE '%car %';
here it is..
SELECT * FROM table WHERE name LIKE '% car' or name LIKE 'car %' or name LIKE '% car %' or name like '% car.';
it will select both start word last word and the between word
If you want to include "car" and exclude "card" you can search for
SELECT Text FROM Table WHERE Text LIKE '% Car.%' or Text LIKE '% Car %';
That would require there to be always a "." in the end.
You can add another
or Text LIKE '% Car';
if you are not sure about this.
If this gets more complicated, you might want to look into regular expressions though. While often slower, they will be faster if your LIKE... OR... expression gets too complicated. You might have to do some benchmarking to find out which is best in your situation.