This probably has something to do with my understanding of full text search or perhaps a known issue. My Java code creates search term for full text search on MySQL like this -
+word*
This works fine if the value in the DB column contains more text than the word itself. However, if the value is exact - no result are returned. I expected it to return results when value is an exact match. Let me give an example -
Assume that DB has column name with value "Manish", now if I search with this -
+Manis*
It does return the result correctly, but if I try this -
+Manish*
It doesn't return any result, though exact match exists in DB column - Name. How can I obtain the desired behaviour in both these cases? Removing + sign works, but it returns far too many results when there are two or more words.
Any help, pointers would be highly appreciated! I am using MySQL 5.0
TIA,
- Manish
Try removing the +
+Man searches for Man but not Manish
Man* searches for Man and Manish.
Related
I am trying to make sort of a search engine that searched through JSON values in my Database.
I have a table with a column called data in data there is a JSON string, example:
{"type_geld":"cash","bedrag":15.0,"totaal":8899.0,"reden":"itemshop-bought-item","citizenid":"EHT44095","steamnaam":"Finn"}
Now I want to search through the key steamnaam I am currently using this query:
SELECT * FROM logs_1 WHERE JSON_CONTAINS(lower(`data`), '"finn"', "$.steamnaam")
This does give me the rows that contain finn as a value in the steamname JSON.
But now I want to also make it check if it's not exactly the same, but almost the same. So basically a LIKE search. Can I achieve this with JSON_CONTAINS or something like that?
So if I type fin instead of finn I also want it to list the rows because it almost matches finn.
I tried a lot of things, but could not figure it out, hope someone has the solution for me! Thank you.
The solution I found:
Apparently after googling a bit more, I found this query, that exactly does what I want:
SELECT * FROM logs_1 WHERE JSON_EXTRACT(lower(`data`), "$.steamnaam") LIKE "%fin%"
The only concern I have if this will stay fast with a lot of rows..
SELECT *
FROM logs_1
WHERE data->>'$.steamnaam' LIKE '%fin%' /* COLLATE according CI collation */
I have been trying to teach myself MySQL, and was wondering if somebody could please explain the difference between the use of the regex metacharacters '*' and '?'. The book I am using describes them both as "matching (0) or one instances of the strings preceding it". I tried using both while looking for the same thing in a practice table I created and got exactly the same output, so if one of the operators is supposed to be greedy and the other not, it doesn't look like that is always the case with every table.
Edit 1: I'm including a screenshot of the output I got from '*' that shows it matching a statement of the form 'ax*' to just a.
Edit 2: regex101.com does not list MySQL as a "flavor" and when I try to do 'al*' to Alexandra there, it says no match for any of the flavors. Is the fact that MySQL Workbench is returning Alexandra as one of the outputs something specific to MySQL that does not apply to any of these other languages?
They are not quite the same. "*" means "0 or more". "?" means "0 or 1", like "optional". So, given "ax*b" and "ax?b":
Neither will match "a"
Both will match "ab"
Both will match "axb"
Only the first will match "axxb" or "axxxxxxb"
I'm trying to search through a database of sofware titles for those that have an interior capital letter (e.g PowerPoiint, inCase).
I tried
select * from table where field REGEXP '^([a-z][A-Z]+)+$'
This seemed to work as it returned a subset of the table and most were correct but a fair amount were not (e.g Alias). Clearly it is doing something right but not sure what; could it be that the ascii is somehow messed up?
Try this as your RegEx pattern:
^[A-z]+[A-Z][A-z]+$
It will match all the examples above (PowerPoint, inCase), and not match 'Alias', one of the examples that you are having trouble with.
Is there a way to do a MySQL query for data fields that start with but may not fully contain a given string?
For instance, if I had the following list of data items:
my_table
1. example.com
2. example.com/subpage
3. subdomain.example.com
4. ain.example.com
5. ple.com
I would like to feed
"example.com/subpage" and return #1, #2
"example.com" and return #1
"wexample.com" and return nothing
"exa" and return nothing
"subdomain.example.com/subpage" and return #3
Thanks a lot!
Given:
CREATE TABLE paths ( path VARCHAR(255) NOT NULL );
Searching for "example.com/subpage" would require the following query:
SELECT * FROM paths WHERE INSTR("example.com/subpage", path) = 1;
Just don't try to run it over a large dataset frequently...
Docs: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
Since your test data indicates you don't want character-by-character matching (but something more like component by component), split the input into the components and search on all prefixes.
If you want to return results for example.com but not exam, you are NOT searching for something that "starts with" yuour input. Not sure if the question is wrong or the examples there.
If the examples are correct, you're going to need to do something to identify if your input is a URL or not using pattern matching like regex or at least specify some solid rules around what you want to match. You'll probably need to explain those rules before a correct recommendation can be made too.
It might be as simple as extracting anything before the "/" if there is one or using your application to break up your request to a url component and a path component.
Mode info on regex in mysql
It seems that you want the column value to match the start of your pattern:
SELECT * FROM my_table WHERE 'example.com' LIKE CONCAT(my_table.my_column, '%');
The downside of this is that it isn't going to use any indexes on my_column.
I have been having trouble searching through a MySQL table, trying to find entries with the character (UTF-16 code 200E) in a particular column.
This particular code doesn't have a glyph, so it doesn't seem to work when I try to paste it into my search term. Is there a way to specify characters as their respective code point instead for a query?
Thanks,
-Ben
Not tested, but CHAR() could work for this:
CHAR(0x200E);
I can't set up a full test case right now, let us know whether it worked.