SQL Like query for each word containing keyword - mysql

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?

Related

How to get exact search result using MYSQL LIKE operator

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.

Search for records in my db which match/contain certain WHOLE keyword(s)

I am looking for a way to search for records in my db which match/contain a certain keyword(s). Now these keyword(s) have to be its own word and not part of another word. So if the keyword is "wich", I don't want it to match records with sand"wich" in the title.
The only way I can think of doing this is by using LIKE but I can't even get that to work.
I tried using the following:
SELECT * FROM tbl_recipes WHERE title LIKE '%$term%'
but that matches all records where $term appears anywhere in the title regardless of whether its part of another word or not.
So I thought this might work
SELECT * FROM tbl_recipes WHERE title LIKE '% $term %'
but this never returns any records.
can anyone see where I'm going wrong? or if there is a better, more suitable function other than LIKE that I should use?
If I wanted to locate rows that had an occurrence of the word 'soup' contained in the title column, which was not part of another word, I could do something like this:
SELECT r.*
FROM tbl_recipies r
WHERE r.title LIKE 'soup'
OR r.title LIKE 'soup %'
OR r.title LIKE '% soup'
OR r.title LIKE '% soup %'
That checks for a match 1) entire title, 2) as first word in title, 3) as last word in title, 4) as word within the title.
This assumes that "words" within the title are delimited by one or more spaces, not commas, dashes, or periods.
MySQL also has a REGEXP function that can perform matches using regular expressions. You would still need to do the same kind of checks.
Another approach would be to use the FIND_IN_SET function. To use that, you could replace all space characters with a comma.
SELECT r.*
FROM tbl_recipes r
WHERE FIND_IN_SET('soup',REPLACE(r.title,' ',','))
This approach lends itself to replacing characters other than space with commas; we could also replace dashes with commas, and tabs with commas, and so on, by wrapping the REPLACE expression in other REPLACE expressions.
if you use mysql use this Query
SELECT * FROM tbl_recipes WHERE title like '% soup %'
contains is not a mysql function.

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.

mysql query to match sentence against keywords in a field

I have a mysql table with a list of keywords such as:
id | keywords
---+--------------------------------
1 | apple, oranges, pears
2 | peaches, pineapples, tangerines
I'm trying to figure out how to query this table using an input string of:
John liked to eat apples
Is there a mysql query type that can query a field with a sentence and return results (in my example, record #1)?
One way to do it could be to convert apple, oranges, pears to apple|oranges|pears and use RLIKE (ie regular expression) to match against it.
For example, 'John liked to eat apples' matches the regex 'apple|orange|pears'.
First, to convert 'apple, oranges, pears' to the regex form, replace all ', ' by '|' using REPLACE. Then use RLIKE to select the keyword entries that match:
SELECT *
FROM keywords_table
WHERE 'John liked to eat apples' RLIKE REPLACE(keywords,', ','|');
However this does depend on your comma-separation being consistent (i.e. if there is one row that looks like apples,oranges this won't work as the REPLACE replaces a comma followed by a space (as per your example rows).
I also don't think it'll scale up very well.
And, if you have a sentence like 'John liked to eat pineapples', it would match both of the rows above (as it does have 'apple' in it). You could then try to add word boundaries to the regex (i.e. WHERE $sentence RLIKE '[[:<:]](apple|oranges|pears)[[:>:]]'), but this would screw up matching when you have plurals ('apples' wouldn't match '[wordboundary]apple[wordboundary]').
Hopefully this isn't more abstract than what you need but maybe good way of doing it.
I haven't tested this but I think it would work. If you can use PHP you can use str_replace to turn the spaces into keyword LIKE '%apple%'
$sentence = "John liked to eat apples";
$sqlversion = str_replace(" ","%' OR Keyword like '%",$sentence );
$finalsql = "%".$sqlversion."%";
the above will echo:
%John%' OR Keyword like '%liked%' OR Keyword like '%to%' OR Keyword like '%eat%' OR Keyword like '%apples%
Then just combine with your SQl statement
SQL ="SELECT *
FROM keywords_table
WHERE Keyword like" . $finalsql;
Storing comma delimited data is... less than ideal.
If you broke up the string "John liked to eat apples" into individual words, you could use the FIND_IN_SET operator:
WHERE FIND_IN_SET('apple', t.keywords) > 0
The performance wouldn't be great - this operation is better suited to Full Text Search.
I'm not aware of any direct solution to that type of query. But Full Text Search is a possibility. If you have a full-text index on the field of interest then a search with OR between each word in the sentence (although I think the OR operator is implied) would find that record ... but it might also find more than you want too.
I really don't think what you are looking for is completely possible but you can look into Full Text Search or SOUNDEX. SOUNDEX, for example, can do something like:
WHERE SOUNDEX(sentence) = SOUNDEX('%'+keywords+'%');
I have never tried it in this context but you should and let me know how it works out.