Search prob in mysql query - mysql

Is there any way to search like below criteria in mysql?
If any the pl reply.
if i search with "murray's" then it then it will return fine data for "murray's" but it should also return data for "murrays" means same word without apostrophy(').
same way if i search without apostrophy('), the it will search also with apostrophy(').
at last
if search query is "murray's", the it will return "murray's" and "murrays" also.
and
if search query is "murrays", the it will return "murrays" and "murray's" also.
Thanks in advance

Your best bet is to create a Full Text Index on your table.
You can then query it as:
select *
from restaurants
where match(name) against ('murrays')
Barring that, you can use SOUNDEX or SOUNDS LIKE in your query. The following two queries are exactly identical:
select *
from restaurants
where soundex(name) = soundex('murrays')
select *
from restaurants
where name sounds like 'murrays'
Also note that MySQL uses the original SOUNDEX algorithm, not the more recent one. Therefore, it will return arbitrary length strings. If you've used SOUNDEX before, just make sure you take that into account.

Related

How can I find a list of rows which contain a string similar to "Butterflies" in MySQL? [duplicate]

I have a table dictionary which contains a list of words Like:
ID|word
---------
1|hello
2|google
3|similar
...
so i want if somebody writes a text like
"helo iam looking for simlar engines for gogle".
Now I want to check every word if it exists in the database, if not it should
get me the similar word for the word. For example: helo = hello, simlar = similar, gogle = google.
Well, i want to fix the spelling errors. In my database i have a full dictionary of all english words. I coudn't find any mysql function which helps me. LIKE isn't helpfull in my situation.
you can use soundex() function for comparing phonetically
your query should be something like:
select * from table where soundex(word) like soundex('helo');
and this will return you the hello row
There is a function that does roughly want you want, but it's intensive and will slow queries down. You might be able to use in your circumstances, I have used it before. It's called Levenshtein. You can get it here How to add levenshtein function in mysql?
What you want to do is called a fuzzy search. You could use the SOUNDEX function in MySQL, documented here:
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex
You query would look like:
SELECT * FROM dictionary where SOUNDEX(word) = SOUNDEX(:yourSearchTerm)
... where your search term is bound to the :yourSearchTerm parameter value.
A next step would be to try implementing and making use of a Levenshtein function in MySQL. One is described here:
http://www.artfulsoftware.com/infotree/qrytip.php?id=552
The Levenshtein distance between two strings is the minimum number of
operations needed to transform one string into the other, where an
operation may be insertion, deletion or substitution of one character.
You might also consider looking into databases that are aimed at full text searching, such as Elastic Search, which provides this natively:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

Search in multiple column with at least 2 words in keyword

I have a table which store some datas. This is my table structure.
Course
Location
Wolden
New York
Sertigo
Seatlle
Monad
Chicago
Donner
Texas
I want to search from that table for example with this keyword Sertigo Seattle and it will return row number two as a result.
I have this query but doesn't work.
SELECT * FROM courses_data a WHERE CONCAT_WS(' ', a.Courses, a.Location) LIKE '%Sertigo Seattle%'
Maybe anyone knows how to make query to achieve my needs?
If you want to search against the course and location then use:
SELECT *
FROM courses_data
WHERE Course = 'Sertigo' AND Location = 'Seattle';
Efficient searching is usually implemented by preparing the search string before running the actual search:
You split the search string "Sertigo Seattle" into two words: "Sertigo" and "Seattle". You trim those words (remove enclosing white space characters). You might also want to normalize the words, perhaps convert them to all lower case to implement a case insentive search.
Then you run a search for the discrete words:
SELECT *
FROM courses_data
WHERE
(Course = 'Sertigo' AND Location = 'Seattle')
OR
(Course = 'Seattle' AND Location = 'Sertigo');
Of course that query is created using a prepared statement and parameter binding, using the extracted and trimmed words as dynamic parameters.
This is is much more efficient than using wildcard based search with the LIKE operator. Because the database engine can make use of the indexes you (hopefully) created for that table. You can check that by using EXPLAIN feature MySQL offers.
Also it does make sense to measure performance: run different search approaches in a loop, say 1000 times, and take the required time. You will get a clear and meaningful example. Also monitoring CPU and memory usage in such a test is of interest.

MySQL storing list of values

I have 300,000 rows in my db and I am doing a full text search like this:
select * from CT
where match (`Abstract`)
against ('tissue engineering' in natural language mode);
Right now it is checking to see if the word "tissue engineering" is found in the abstract, however, I have a list of around 50 terms I need to check for. My first response is to create a list of all the values and check it against the list. Is this possible with MySQL?
#How to write this in MySQL?
terms = ['abdomen', 'abdominal', 'acl'...'vertebral', 'waist', 'wrist']

MySQL search for keywords containing apostrophe or not

I have a MySQL database containing a list of UK towns and one of which is "Connah's Quay".
I want to be able to return results for "Connah's Quay" whether I have used the apostrophe or not, so "Connah's Quay" and "Connahs Quay" returns "Connah's Quay".
Rather than creating a field containing both versions (one with and another without the apostrophe), is there a SQL query I can run that will return results whether I have used the apostrophe or not?
QUERY:
SELECT * FROM uk_postcodes WHERE postal_town LIKE "connahs%";
Standard approach would be to normalise the data and search on that, so something like:
SELECT * FROM uk_postcodes WHERE REPLACE(postal_town, '''', '') LIKE 'connahs%';
This is a bit horrible to do on the fly (and not index friendly), so you would be better to store on table (also means you can also then cope with "Stow-cum-Quy" vs. "Stow cum Quy", etc.)
You might try this:
SELECT *
FROM uk_postcodes
WHERE REPLACE(postal_town,"'","") LIKE CONCAT(REPLACE("connah's","'",""),"%");
This removes the apostrophes from both the search term and the column value before the comparison.

How to use prefix wildcards like '*abc' with match-against

I have the following query :
SELECT * FROM `user`
WHERE MATCH (user_login) AGAINST ('supriya*' IN BOOLEAN MODE)
Which outputs all the records starting with 'supriya'.
Now I want something that will find all the records ending with e.g. 'abc'.
I know that * cannot be preappended and it doesn't work either and I have searched a lot but couldn't find anything regarding this.
If I give query the string priya ..it should return all records ending with priya.
How do I do this?
Match doesn't work with starting wildcards, so matching with *abc* won't work. You will have to use LIKE to achieve this:
SELECT * FROM user WHERE user_login LIKE '%abc';
This will be very slow however.
If you really need to match for the ending of the string, and you have to do this often while the performance is killing you, a solution would be to create a separate column in which you reverse the strings, so you got:
user_login user_login_rev
xyzabc cbazyx
Then, instead of looking for '%abc', you can look for 'cba%' which is much faster if the column is indexed. And you can again use MATCH if you like to search for 'cba*'. You will just have to reverse the search string as well.
I believe the selection of FULL-TEXT Searching isn't relevant here. If you are interested in searching some fields based on wildcards like:
%word% ( word anywhere in the string)
word% ( starting with word)
%word ( ending with word)
best option is to use LIKE clause as GolezTrol has mentioned.
However, if you are interested in advanced/text based searching, FULL-TEXT search is the option.
Limitations with LIKE:
There are some limitations with this clause. Let suppose you use something like '%good' (anything ending with good). It may return irrelevant results like goods, goody.
So make sure you understand what you are doing and what is required.