Replacing wildcard %LIKE% query - mysql

I have a file-path search, allowing a user to search his/her files on a website. Example entries are:
`path`
- /Volumes/Fulfill/03-01-13/FILFILE.txt
- /Volumes/Master/.Trash/Weeds.mov
If someone search for "master mov" it would do the following query:
SELECT * FROM files WHERE path LIKE '%master%' AND path LIKE '%mov%'
and would return:
- /Volumes/Master/.Trash/Weeds.mov
What would be a better way or method to do the above search?
Update: I tried doing the following, but my implementation of the MYISAM FULLTEXT search didn't work well at all:
select * from path where path like '%red%' and path like '%state%' <-- works
select * from path where match(path) against ('red state'); <-- zero results
Is there another way to implement full text search than the above?

% is a wildcard. It will be faster if you replace it with something like this:
SELECT * FROM files WHERE path LIKE '/Volumes/Master%mov'
Because you're not checking for more conditions.
Update
For Full Text option you would (one time)
ALTER TABLE files ADD FULLTEXT(path);
Then for the searches you would
SELECT * FROM files
WHERE MATCH (path)
AGAINST ('+Master +mov' IN BOOLEAN MODE);
Short words are ignored, the default minimum length is 4 characters. You can change the min and max word length with the variables ft_min_word_len and ft_max_word_len

Related

Using regexp with sql to find words that match specific characteristics

I want to write a select statement that gets all the columns where one of them match a certain regular expression.
I basically have this file_name column and I want to select all files that start with a certain word, have a certain word in the middle, and end with 'xlsx'.
I know for checking if if starts with a certain word, I can do
SELECT * FROM files WHERE file_name REGEXP '^company';
to find files that start with the word company, just using that as a place holder.
after this point it can have whatever other characters but at some point in the middle, i am looking for another word, lets say 'dog' and then ends with xlsx.
I know for finding file names that end with xlsx i can do:
SELECT * FROM files WHERE file_name REGEXP 'xlsx[[:>:]]';
My main confusion is how to combine all these 3 cases into one statement. so that it starts with company then at some point has the word dog then after at some point ends with xlsx.
You can combine all conditions into one select using:
SELECT * FROM files WHERE file_name REGEXP '^company.*dog.*xslx$';
If the "words" are not too complex, LIKE will be faster:
SELECT * FROM files WHERE file_name LIKE 'company%dog%xslx';
especially if you have INDEX(file_name).

msyql searching a domain, without the TLD extension

How can we search for a domain, without the TLD in mysql, so for e.g. testdomain.com, I would want to search only testdomain not the .com, so a search for test would return row, but a search for com would not.
I assume it would be similar to below with some regex, but no idea how to achieve that.
SELECT * FROM domains WHERE domain_name LIKE '%$search%'
Any idea on how to to search just that part of the domain?
You can do something like:
SELECT * FROM domains
WHERE SUBSTRING_INDEX(domain_name, '.', 1) LIKE '%$search%'
if you are looking for search a name starting with a string your query must be:
SELECT * FROM domains WHERE domain_name LIKE '$search%'
this query is a good query because it use indexes.
adding the "." character at the end you will find only the full name,
also this query is a good query because it use indexes.
SELECT * FROM domains WHERE domain_name LIKE '$search.%'
Else if you want to make a partial search you need to add the % before and after the term but in this case the "com" search will match, this search is not good becouse it do not use indexes.
At last this expressions search for a string containing the name excluding the TLD, this is not a good query because it do not use indexes.
SELECT * FROM domains WHERE domain_name LIKE '%$search%' and not like '%.$search%'
A good idea could be to split fields in your database, make a column (or an additional colunm) for the domain name without TLD and search in this new coloumn.

Anyway to filter access records containing an '#'?

I have an Access database that is being used for a website. In the DB there is a field for image file names that is used to display images on the site. In some case the person responsible for gathering the images started using the # character in the image file names when saving them and this is causing the images not show on the website.
Is there anyway to filter out just the records where the image field contains the '#' character?
Everything I've tried has Access treating it like a wildcard and picking up any number.
As mentioned in a comment, you can select rows which have # contained in your image_file_name field by checking whether the field is LIKE '*[#]*'
However, since you want to filter out those rows, target the inverse of that pattern match ... NOT LIKE '*[#]*'
A query like this would work within an Access session with default settings:
SELECT y.*
FROM YourTable AS y
WHERE y.image_file_name Not Like '*[#]*';
However, since you're using the Access db to feed a website, you may be using ADO/OleDb to connect to the db file. If that is the case, use % instead of * as the wildcard character:
WHERE y.image_file_name Not Like '%[#]%';
Or you could use Alike instead of Like. In that situation, the wildcard should always be % and the query will work correctly whether you're running it from within or outside of Access:
WHERE y.image_file_name Not ALike '%[#]%';
A totally different approach is to use InStr to find the position of # within image_file_name and select the rows where InStr returns zero:
SELECT y.*
FROM YourTable AS y
WHERE InStr(1, y.image_file_name, '#') = 0;
If you also want rows where image_file_name is Null, you can add that condition to the WHERE clause with OR.

Simple MySQL query not selecting all data?

I know a bit about MySQL, but not enough to know why this query is not working.
SELECT * FROM files WHERE uploader LIKE 'value';
I have a database of files which contains uploader names. Yet, when I search for an uploader name it misses a lot of the entries, even though the name is completely identical all through.
No idea why it does this.
Thanks in advance.
Sonny, you need some wildcards in that there LIKE clause.
SELECT * FROM files WHERE uploader LIKE '%value%';
If you are searching for an identical uploader name, for example, an uploader named Jason, then use the equals comparison operator.
SELECT * FROM files WHERE uploader = 'Jason';
If you would like to match ssJasonsss, Jason, etc, then use:
SELECT * FROM files WHERE uploader LIKE '%Jason%';
Try below :
SELECT * FROM files WHERE uploader LIKE '%value%'
read more details about Pattern Matching
When you use like you usually want to include some sort of wildcard:
SELECT * FROM files WHERE uploader LIKE '%value%';
For undefined number of characters use % for just one additional character use _.
Some examples:
'%value%' will match:
somevaluesome
somevalue
valuesome
value
Another sample:
'_value_' will match:
avaluea
but not
value
avalue
valuea
You can find more wildcards here:
http://dev.mysql.com/doc/refman/5.1/en/string-comparison-functions.html

MySQL - Finding partial strings - Full Text?

I just found a bunch of rogue data in my MYSQL db...
The only way to get to it is via one of the columns - FILE_PATH which contains a slash stripped version of a file path. There are a few rogue files in this set that I need find - they all have the file name "Thumbs.db" but they have a variety of paths
example:
F:DatasetGroupedByFormatsx-fmt-398Thumbs.db
I have a full text index on the field, however the following query doesn't give any returns:
SELECT * FROM main_small WHERE MATCH `FILE_PATH` AGAINST ('Thumbs.db')
Response:
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0382 sec )
I am unsure whether this is because I have the syntax wrong, or whether the text string needs to be isolated by whitespace/punctuation.
Surely it's
select * from main_small where FILE_PATH like '%Thumbs.db'
However, if not then does MySql Full text Search help?
The problem is that your query thinks 'Thumbs.db' is a whole word. You'll need to find some way to do wildcard searching in order to select those rows. How about:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'
Just use LIKE:
SELECT * FROM main_small WHERE `FILE_PATH` LIKE '%Thumbs.db'