mysql regexp for search using alias - mysql

I am not very good with regexp so I really would like some help to achieve my goal.
When searching in my db I use an alias for specific keywords.
Here is an example
keyword tets alias test (someone have spell wrong then word test)
keyword b.m.w alias bmw (if someone write b.m.w instead of bmw)
etc.
So far if a user searches for "bmw 316" I use LIKE "%bmw%316%" to get the results.
Now if the user searches for "b.m.w 316" I must use
"%b.m.w%316%" OR
"%bmw%316%"
because b.m.w has alias bmw.
In the case of 6 words with 2-3 aliases there are too many combinations.
I am trying to achieve it with regexp.
In the scenario above it would be something like (bmw|b.m.w) 316.
How do I solve this problem?

You are not looking for REGEXP you are looking for a thing called levenshtein distance
MySQL does not (yet) have native support for this (wonderful) concept, but you can download a UDF here:
http://joshdrew.com/
And here's a list so you've got something to choose from:
http://blog.lolyco.com/sean/2008/08/27/damerau-levenshtein-algorithm-levenshtein-with-transpositions/
You can also write your own function in MySQL, so you don't have to install a UDF.
http://www.supermind.org/blog/927/working-mysql-5-1-levenshtein-stored-procedure
Finally this question might help you out as well:
Implementation of Levenshtein distance for mysql/fuzzy search?
A query for the closest match would look something like:
SELECT * FROM atable a ORDER BY levenshtein(a.field, '$search') ASC LIMIT 10

Related

Yii2 Sphinx search greater than less or Less than less condition OR "BETWEEN"

$totalGeneralDownload = $squery->from('general_materials','general_materials_delta')
->match
(
(new MatchExpression())
->match([$param => $searchTitle])
->andfilterMatch(['and','download_count>=100','download_count<=100'])
)
->showMeta(true)
->search();
This is my sphinx code, can anyone let me know how to use Between in Sphinx
I have R&D on it and find out that sphinx only text search not giving between type clauses so i think you should get id and then you have to filter by between clauses.
Sphinx not provide between clause so we have to manage in our code.
once you get id from sphinx and then you have to filter by your code and then get the result.

Query for exact word search

hope that you are doing fine
I am having very hard time writing a query
Here is my question explained
i have a database table say "jreviews_content" which has a field named "jr_produits"
In "jr_produits" the data is is the format *ryan-decosta*tom-gosling* so i want a search query that is exact word based i.e if the user type "rya" the mysql should not return anything
but if the user type ryan then it should return the row likewise if the user type "gos" the mysql should not return anything
but if the user type gosling then it should return the row where ryan and gosling are the exact words
the query that i am writing are
SELECT *
FROM `jreviews_content`
WHERE jr_produits LIKE '%*ryan-%' or jr_produits LIKE '%-ryan*%'
or jr_produits LIKE '%*ryan*%' or jr_produits LIKE '%-ryan-%';
I want that to be done in some other way that is more efficient(either by regular expression or any other method)
SELECT * FROM `jreviews_content` WHERE jr_produits REGEXP '^[*-]ryan[*-]$'
It doen't fetch anything
neither does
SELECT * FROM `jreviews_content` WHERE jr_produits like '%[*-]ryan[*-]%'
Please suggest something
Try the MySQL regex word boundary markers. They're documented about halfway down this page:
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]ryan[[:>:]]'
Note that I don't have MySQL access today, so this is untested.
Also heed what #user1032531 said. Records with values like *ryan-decosta*tom-gosling* almost always mean "bad design".

Using HeidiSQL with mysql select statement with "not like" does not work

Ok guys, here's the story, I have a very simple query I'm trying to do and for the life of me I don't understand why it doesn't work.
I'm am using the streaming twitter feed to populate my database using keywords. Now I want to filter out the retweets by using this query
select * from earthquake
where earthquake.Text not like '%RT%'
order by earthquake.Text
It returns "0 rows affected, 0 rows found", and yes there are thousands of retweets, so I know they exist.
I do the same query with the '%#%' and it finds thousands as expected, it almost seems that everything except 'RT' works.
Did you try NOT LIKE '%RT #%'?
Is it possible that common word xxxrtxxx always contains in your Text field?
I don't know anything about mysql specifically, but is there a function like MSSQL's PatIndex() that you could use instead of not like?
Alternatively you could try something like this: (dont know if your db supports this syntax)
SELECT * FROM Earthquake
WHERE CASE WHEN Text LIKE '%RT%' THEN 1 ELSE 0 END = 0
ORDER BY Text
I'd also advise checking your logic of looking for "RT" somewhere in the Text column to identify the stuff you don't want, because I'm surprised that "Not Like [expression]" apparently doesnt work.

What is the "Rails Way" of doing a query with an OR clause using ActiveRecord?

I'm using Rails 3 with a MySQL database, and I need to programmatically create a query like this:
select * from table where category_name like '%category_name_1%'
OR category_name like '%category_name_2%'
(...snip...)
OR category_name like '%category_name_n%'
Given the table size and the project scope (500 rows at most, I think), I feel that using something like thinking sphinx would be overkill.
I know I could simply do this by writing the query string directly, but wanted to know if there's an ActiveRecord way to do this. There's no mention of this on the official guide, and I've been googling for a long while now, just to end empty-handed :(
Also, is there a reason (maybe a Rails reason?) to not to include the OR clause?
Thanks!
Assuming you have an array names with category names:
Model.where( names.map{"category_name LIKE ?"}.join(" OR "),
*names.map{|n| "%#{n}%" } )
you should google first, there is already an answer.
Look here and then here
and you'll get something like this:
accounts = Account.arel_table
Account.where(accounts[:name].matches("%#{user_name}%").or(accounts[:name].matches("%#{user_name2}%")))
If you look at the guide, they have examples that can easily be modified to this:
Client.where("orders_count = ? OR locked = ?", params[:orders], false)
Mysql has a regexp function now that can clean things up a bit, assuming there's no regex metachars in your category names:
Table.where "category_name regexp '#{names.join('|')}'"

Find exact amount of chars with MySQL

My teacher is pointing to oracle and debating that, but I have hopes of doing this with a pure injection in MySQL. I want to search the database in phpMyadmin and find exactly two K's. Is this possible?
The nearest thing I got is:
SELECT etunimi, sukunimi FROM nimet WHERE sukunimi LIKE '%k%k%n';
It is the last %k%k%n that needs to be solved. Can you help me prove my teacher wrong?
Use MySQL's REGEXP operator:
SELECT etunimi, sukunimi
FROM nimet
WHERE sukunimi REGEXP '^[^k]*k[^k]*k[^k]*n$'
I'm not sure what the n at the end is for, since you don't mention it in your question, but I left it, just in case.
Use this to find out strings with exactly 2 k's
SELECT etunimi, sukunimi FROM nimet WHERE LENGTH(sukunimi)-LENGTH(REPLACE(sukunimi, 'k', ''))=2;