Search in multiple column with at least 2 words in keyword - mysql

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.

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

Reverse of soundex in mysql

select SOUNDEX('ram')
WILL RETURN 'R500'
Is there a way to input 'R500' and return 'ram'
something like this
select reverse_of_SOUNDEX('R500')
RESULT
--------
ram
You have the same problem as you would have for trying to reverse any many-to-one mapping function.
While ram may be one word that gives you the soundex code r500, so do a large number of other words (ran, rim, run, rune, rain and so on, depending on which variant of soundex you're using).
So which word would you like to see displayed in that case?
One possibility, if you're after a list of words, would be to populate a table with a large number of words (from a dictionary somewhere) along with their soundex codes. The codes can be set, after insertion of the words, quite easily with:
update sxmap set sxcode = soundex(word)
and then you can simply do:
select word from sxmap where sxcode = 'R500'
or:
select word from sxmap where sxcode = soundex('ram')
A Soundex mapping is essentially a Hash code (i.e a many-to-one mapping).
As such it is not uniquely reversible.
There are several words that can soundex map to R500, you cannot tell which was the original before mapping.

MySQL - FULLTEXT in BOOLEAN mode + Relevance using views field

I have the following table:
CREATE TABLE IF NOT EXISTS `search`
(
`id` BIGINT(16) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`string` TEXT NOT NULL,
`views` BIGINT(16) NOT NULL,
FULLTEXT(string)
) ENGINE=MyISAM;
It has a total of 5,395,939 entries. To perform a search on a term (like 'a'), I use the query:
SELECT * FROM `search` WHERE MATCH(string) AGAINST('+a*' IN BOOLEAN MODE) ORDER BY `views` DESC LIMIT 10
But it's really slow =(. The query above took 15.4423 seconds to perform. Obviously, it's fast without sorting by views, which takes less than 0.002s.
I'm using ft_min_word_len=1 and ft_stopword_file=
Is there any way to use the views as the relevance in the fulltext search, without making it too slow? I want the search term "a b" match "big apple", for example, but not "ibg apple" (just need the search prefixes to match).
Thanks
Since no one answered my question, I'm posting my solution (not the one I would expect to see if I was googling, since it isn't so easy to apply as a simple database-design would be, but it's still a solution to this problem).
I couldn't really solve it with any engine or function used by MySQL. Sorry =/.
So, I decided to develop my own software to do it (in C++, but you can apply it in any other language).
If what you are looking for is a method to search for some prefixes of words in small strings (the average length of my strings is 15), so you can use the following algorithm:
1. Create a trie. Each word of each string is put on the trie.
Each leaf has a list of the ids that match that word.
2. Use a map/dictionary (or an array) to memorize the informations
for each id (map[id] = information).
Searching for a string:
Note: The string will be in the format "word1 word2 word3...". If it has some symbols, like #, #, $, you might consider them as " " (spaces).
Example: "Rafael Perrella"
1. Search for the prefix "Rafael" in the trie. Put all the ids you
get in a set (a Binary-Search Tree that ignores repeated values).
Let's call this set "mainSet".
2. Search for the prefix "Perrella" in the trie. For each result,
put them in a second set (secSet) if and only if they are already
in the mainSet. Then, clear mainSet and do mainSet = secSet.
3. IF there are still words lefting to search, repeat the second step
for all those words.
After these steps, you will have a set with all the results. Make a vector using a pair for the (views, id) and sort the vector in descending order. So, just get the results you want... I've limited to 30 results.
Note: you can sort the words first to remove those with the same prefix (for example, in "Jan Ja Jan Ra" you only need "Jan Ra"). I will not explain about it since the algorithm is pretty obvious.
This algorithm may be bad sometimes (for example, if I search for "a b c d e f ... z", I will search the entire trie...). So, I made an improvement.
1. For each "id" in your map, create also a small trie, that will
contain the words of the string (include a trie for each m[id]...
m[id].trie?).
Then, to make a search:
1. Choose the longest word in the search string (it's not guaranteed,
but it is probably the word with the fewest results in the trie...).
2. Apply the step 1 of the old algorithm.
3. Make a vector with the ids in the mainSet.
4. Let's make the final vector. For each id in the vector you've created
in step 3, search in the trie of this id (m[id].trie?) for all words
in the search string. If it includes all words, it's a valid id and
you might include it in the final vector; else, just ignore this id.
5. Repeat step 4 until there are no more ids to verify. After that, just
sort the final vector for <views, id>.
Now, I use the database just as a way to easily store and load my data. All the queries in this table are directly asked to this software. When I add or remove a record, I send both to the DB and to the software, so I always keep both updated. It costs me about 30s to load all the data, but then the queries are fast (0.03s for the slowest ones, 0.001s in average; using my own notebook, didn't try it in a dedicated hosting, where it might be much faster).

using LIKE operator in mysql

I want search companies from my company table when i give company name...here am using like operator
eg: saravana stores
it gives the result saravana stores texttiles,saravana stores thanga maligai,etc(which is contained with saravana stroes...coz of using LIKE operator)
Now my problem is when i give lcd projectors in the companyname, also want to fetch the records which are contained with the only projector word...but like operator gave the results with the 'lcd projector'
am making clear?
Try:
WHERE (name LIKE '%saravana%' OR name LIKE '%stores%')
This has two disadvantages:
It can't use an index so it will be slow.
It can give you matches you don't want like 'bestorest' matches '%stores%'.
You might want to use a full text search instead. You could also consider an external engine such as Lucene.
If you want proper fultext search, I highly recommend trying Lucene or Sphinx.
I know it would get a little complicated, but it's worth it for the end result.
Mark Byers is right.
To get more efficiency
After query dividing to words you can modify search input to get word base and unify searching to get smth lika:
WHERE (name LIKE '%sarava%' OR name LIKE '%stor%')

Search prob in mysql query

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.