I've been searching all over and trying out different approaches but I'm just not getting what I need.
Is there a possibility in MySQL to select a db entry similar to a string value entered in a form?
for example: I have a db with vendor names in it and a customer can enter a vendor name into a search field. Let's say he's looking for adobe but accidentally types 'adope' in the search field. I would now like to select all entries, that are similar to 'adope'. How can I do that?
I've tried ... LIKE '%$vendor%' and all kinds of regexp but it seems I'm on the wrong way...
Thanks fpr your help in advance :-)
Cheers
Fred
You can do with SOUNDEX, check out this tutorial:
Mysql function to soundex match a word in a multi word string
Also check out the official docs
Returns a soundex string from str. Two
strings that sound almost the same
should have identical soundex strings.
A standard soundex string is four
characters long, but the SOUNDEX()
function returns an arbitrarily long
string. You can use SUBSTRING() on the
result to get a standard soundex
string.
Related
I have an JSON string stored in the database and I need to SQL COUNT based on the WHERE condition that is in the JSON string. I need it to work on the MYSQL 5.5.
The only solution that I found and could work is to use the REGEXP function in the SQL query.
Here is my JSON string stored in the custom_data column:
{"language_display":["1","2","3"],"quantity":1500,"meta_display:":["1","2","3"]}
https://regex101.com/r/G8gfzj/1
I now need to create a SQL sentence:
SELECT COUNT(..) WHERE custom_data REGEXP '[HELP_HERE]'
The condition that I look for is that the language_display has to be either 1, 2 or 3... or whatever value I will define when I create the SQL sentence.
So far I came here with the REGEX expression, but it does not work:
(?:\"language_display\":\[(?:"1")\])
Where 1 is replaced with the value that I look for. I could in general look also for "1" (with quotes), but it will also be found in the meta_display array, that will have different values.
I am not good with REGEX! Any suggestions?
I used the following regex to get matches on your test string
\"language_display\":\[(:?\"[0-9]\"\,)*?\"3\"(:?\,\"[0-9]\")*?\]
https://regex101.com/ is a free online regex tester, it seems to work great. Start small and work big.
Sorry it doesn't work for you. It must be failing on the non greedy '*?' perhaps try without the '?'
Have a look at how to serialize this data, with an eye to serializing the language display fields.
How to store a list in a column of a database table
Even if you were to get your idea working it will be slow as fvck. Better off to process through each row once and generate something more easily searched via sql. Even a field containing the comma separated list would be better.
I have a database where office users have created a "poo man's categorization" by prefixing the administrative title field with a category. For instance, you have records like
Applications - When to Apply
Applications- Fees
Admission: GPA requirements
Admissions: Bursar
We are adding a category column, and I want to get (as close as possible) all the unique user-created categories in the title field. From the examples above, Applications, Admission, and Admissions are good enough.
How can I write a query to return the first part of a field, split on the first non-alphahnumeric character?
AFAIK, this isn't possible with any of the built-in MySQL functions. There's no function for searching a string for a character outside a set, e.g. the first non-alphanumeric character.
You can write a stored function that does it, by looping over the string and calling SUBSTR(). But you're probably better off searching the net for a user-defined function that can split a string using a regular expression.
One column returns such values:
Something";s:5:"value";s:3:"900";s:11:"print_
I want to extract all numbers that are at least 3 digits long, in the above case thats 900. How can I do that in MySQL? Maybe using a regex? I cant use any index, the length of the string and the number in the string can be different.
Thanks!
Try unserialize() it if you are using PHP! And then var_dump it to see the strings and arrays
You can't extract them using MySQL, use any other language for that.
What you can do is include a Where Clause, that will make the work easier for your script.
Assuming your column is called "serialized" in the table "example"
SELECT serialized FROM example WHERE serialized REGEXP '[0-9]{3,}'
Please note that REGEXP is just outputting 1 or 0
After you did the query, use the regex functions of your language do extract the numbers like so:
([0-9]{3,})*
Using full text search in mysql I'd like to have exact phrase:
"romantic dinner" to be found.
But I also would like each of the words could have synonyms like:
"romantic dinners" to be found for example (our language has great problem where every word has 8 endings like)...
I tried:
+"romantic (dinner dinners)" and
"romantic +(dinner dinners)"
but nothing seems to get results... Is it possible to make some logical OR inside exact search?
UPDATE: TO make it one sentance question: Is there a way to put some logical operators in exact match ("") in full text search?
It will only partially your problem, but there is a soundex() function in mysql which transform the given string to a soundex representation. Similars string should have the same soundex representation so it's maybe a start.
Hope this helps.
If '"roman* dinn*" doesn't work for you, it might be time to look into something like Solr: http://lucene.apache.org/solr/ which will allow for more sophisticated searches, and might already have a stemmer for your language.
SELECT field_name FROM table_name WHERE MATCH(field_name) AGAINST('romantic* dinner*' IN BOOLEAN MODE)...
definitely you can use + and - to further refining your results
reference: http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html
Is it possible to get the (first?) match of a regex and output it within a select? It looks like the REGEXP function only return whether there has been a match or not. I want to be able to extract information out of a varchar column without having to use complex SUBSTRING-LOCATION nestings.
Any ideas?
http://dev.mysql.com/doc/refman/5.1/en/regexp.html that's all there is. You can't do more than pattern comparison.