specific search in database - mysql

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.

Related

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?

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.

Complex mysql search query, 'boolean and' search of keyword on mysql string

Well I needed to implement some search parameters on my applications, and could not come up with any better solution so i hope you guys could help me, my prob goes something like this->
i have a table with following columns,
id,question
now i am supposed to search keywords, with various criterias, such as->
If i search keyword "heart disease" the returned questions should contain both words "heart" and "disease"
Sentence like " We have a heartly disease" are returned because "heartly" contains "heart", but sentence like "We have a fooheart disease" won't be returned cause "foo" is before "heart" and that isn't acceptable according to the criteria given. But anything following "heart" or "disease" is acceptable.
Well these were the criterias given, I know my english isn't that impressive and haven't been able to explain my problem properly. But i do hope for a solution!! Thanks!!
You probably would be better off with a full text search engine like Lucene, but you can do it in mysql. You would just have to build up the search criteria based on the number of words. For many cases, this would be an incredibly inefficient query.
something like
select * from table
where text like '% heart%' and text like '% disease%'
Link to SQLFiddle
should work.
Note that this isn't necessarily the full solution. The following value wouldn't be returned, because there would be no space before diseases or heart.
Diseases are bad.Hearts are very susceptible.
The problem, of course, is that you are going to have to start building up a lot of special cases. To address the comments, and the example I showed, you would have to add in rules like:
select * from terms
where (terms like '% heart%' or terms like 'heart%' or terms like '%.Heart%')
and (terms like '% disease%' or terms like 'disease%' or terms like '%.disease%')
Link to more advanced case
You could also do this with some sort of regular expression. This would handle the cases that you've brought up.
select * from terms
where (terms like 'heart%' or terms REGEXP '[ |\.]heart')
and (terms like 'disease%' or terms REGEXP '[ |\.]disease')
Example with regular expressions

Mysql SELECT query on name ignoring the first word if it is "the", "a", "an" etc

I've been trying (without success) to construct a MYSQL query which will select a group of records with a "title" field starting with a single alphabetical character but ignoring the first word if it's "The", "An" or "A". I've found plenty of examples that do this for the ORDER BY part of the query, but it's the initial WHERE part that I need to do it for, as the order is irrelevant if the correct records haven't been found. Using
WHERE title LIKE "R%"
will just give me titles that have this as the very first letter (e.g. "Robin Hood") but won't match "The Red House". I think I need some kind of REGEX, but I can't seem to get it to work.
So for example, given the following movie titles,
Road House
The Return of the King
Mamma Mia
Argo
Titanic
A River Runs Through it
Selecting movie titles that start with "R" would return the following:
The Return of the King
A River Runs Through it
Roadhouse
(other fields omitted)
The easiest way is to programmatically expand the query to something like
SELECT
...
WHERE
title LIKE 'R%'
OR title LIKE 'The R%'
OR title LIKE 'A R%'
OR title LIKE 'An R%'
...
This should perform better than a REGEX, as it will be able to use an index, which a REGEX never will.
BTW: The canonical way to do this, is to store the article in a seperate field.
This regex should suit your needs:
WHERE title REGEXP '^(The |An? )?R.*$'
But as #EugenRieck noticed, since you probably use an index on the title column, you should better use the WHERE... OR... clauses.
To add to the above suggestions (both of which I agree with), for the sort you would probably need to use a CASE to dervive a field for the ORDER BY clause.
SELECT somefield,
CASE
WHEN title LIKE 'R%' THEN title
WHEN title LIKE 'The R%' THEN SUBSTRING(title FROM 5)
WHEN title LIKE 'A R%' THEN SUBSTRING(title FROM 3)
WHEN title LIKE 'An R%' THEN SUBSTRING(title FROM 4)
ELSE title
END AS SortTitle
FROM sometable
ORDER BY SortTitle