Simple MySQL query not selecting all data? - mysql

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

Related

Sql select url regex

So I have to find the urls which contains the word 'foo' for example . I want to do this with a regex like SELECT * FROM table WHERE url = regex. The url is https//... OR http://.... and I need to find the word foo for example in the url. What I actually want is a regex which will check if foo is in the hostname or in the path. Thanks for help!
EDIT :
I want to find a faster alternative with regex because LIKE '%foo%' is very slow on my table.
Use LIKE with the % wildcard operator.
SELECT * FROM table WHERE url LIKE '%foo%'

MySQL LIKE - partial match with up tp 2 different characters

Trying to look for partial matches:
SELECT * FROM watches_check WHERE modelNumber LIKE
'URL'
sample data:
URL: http://www.domain.com/s/keywords=AK-1470GBST&id=123456
modelNumber: AK-1470GBST
I'd like to find if the modelNumber is in URL field but it returns 0 results all the time.
Where did I go wrong?
Also, would it be possible to find partial matches, where there is say 1 or 2 characters different, for example AK-1470GBST vs AK/1470GBST ('/' instead of '-') will be considered a match
You can use
SELECT * FROM watches_check WHERE INSTR(URL, modelNumber) > 0
to check if your url contains the modelNumber. This will even work, if your modelNumber contains the wild cards for LIKE patterns : % and _.
It won't return partial matches. Have a look at Gordons answer for that.
The answer to your first question is to get the SQL correct. I think you want:
SELECT *
FROM watches_check
WHERE URL like concat('%', modelNumber, '%');
Note: The single quotes were removed from "URL" and wildcards were added to the search.
The answer to your second question is a technique called Levenshtein distance (see the Wikipedia page here). If you google "Levenshtein mysql" you will find various implementations of the algorithm.

More efficient than using lots of LIKE queries mysql

I need to query a database to find certain urls containing a certian set of criteria for example : "MY" AND "sand" in any order.
I am currently using LIKE '%MY%' AND LIKE '%Sand%' is there a btter way of executing this?
Thanks
Could try REGEXP e.g:
WHERE url REGEXP '(my.*sand|sand.*my)'
Or alternatively:
WHERE URL REGEXP 'my' AND url REGEXP 'sand'
Not sure how the speed will compare...

How to select all distinct filename extensions from table of filenames?

I have a table of ~20k filenames. How do I select a list of the distinct extensions? A filename extension can be considered the case insensitive string after the last .
You can use substring_index:
SELECT DISTINCT substring_index(column_containing_file_names,'.',-1) FROM table
-1 means it will start searching for the '.' from the right side.
there is A very cool and powerful capability in MySQL and other databases is the ability to incorporate regular expression syntax when selecting data example
SELECT something FROM table WHERE column REGEXP 'regexp'
see this http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/
so you can write pattern to select what you want.
The answer given by #bnvdarklord is right but it would include file names which does not have extensions as well in result set, so if you want only extension patterns use below query.
SELECT DISTINCT substring_index(column_containing_file_names,'.',-1) FROM table where column_containing_file_names like '%.%';

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'