Simple MySQL LIKE not working as expected - mysql

I have a database of information about images. I need to allow users to search for file names that contain a user-specified character string. It's not working the way it appears it should.
For example, the database contains 27 records for files whose names begin with the letter 'b'.
If I execute (in PHPMyAdmin) the query:
SELECT * FROM image WHERE img_name LIKE '%b%';
I get a list of records whose file names contain the letter 'b' (as expected).
But only 4 of the 27 records for files starting with 'b' show up in that list.
Any idea what I'm missing?

After I posted the question, another possibility occurred to me, which turned out to be the solution.
I looked further into the database definition (I did not design it), and it uses varbinary(255) for img_name. If I change the query to:
SELECT * FROM `image` WHERE CONVERT(img_name USING latin1) LIKE '%b%'
I get all of the records that I expect.

You say that the ones that aren't showing up are ones that start with a 'b'.
This could be a case issue.
Convert the name to lower case like so
SELECT * FROM image WHERE strtolower(img_name) LIKE '%b%';

Related

Matching content in two different columns-MySQL

Let's say I have a list of person names and a list of social media URL's (that might or might not contain a portion of the person names).
I'm trying to see if the full name is not contained in the list of URL's I have. I don't think a "not like" would work here (because the URL has plenty of other characters to throw back a result), but I can't think of any other way to address this. Any tips? The closest I could find was from this:
Matching partial words in two different columns
But I'm unsure if that applies here.
Just use SELECT * FROM yourtable WHERE url LIKE '%name%' % means any characters even whitespace. Then just check if it returned any rows.
From mysql doc:
% matches any number of characters, even zero characters.
mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1
So let's say these are your url's in your list:
website.com/peterjohnson
website.com/jackmiller
website.com/robertjenkins
Then if you would do:
SELECT * FROM urls WHERE url LIKE '%peter%'
It would return 1 row.
You can also use NOT LIKE so you will get all the rows not containing the name.

Equal (=) and like in mysql giving different result for same string with no wildcards

Maybe the question was not clear: What makes the difference, based on a character-by-character matching, between the two mail strings provided, as long as both were only [a-z] characters and in the same table (meaning same collation) to explain that some strings fail and some not? Anyone has a clue?
I've found several debates explaining the LIKE and = use in mySql, but not find a fulfilling answer for this issue:
I've found out that searching by whole mailboxes (not truncated, but only complete mailboxes so using no wildcards) using LIKE will not return few of them in my script, but they will match if using = sign instead.
(Of course currently function is updated and working properly with equal sign, but I would appreciate if someone could help me bring some light into this).
I can't reproduce the mailboxes per obvious security reasons but, can reproduce the structure of the last one I noticed that fails (each "x" represents a smallcap latin non-special character [a-z]):
select id from table_name where email = "xxx.xxxxxxxxx.xxxxxx#gmail.com"
Returns the id
select id from table_name where email like "xxx.xxxxxxxxx.xxxxxx#gmail.com"
Returns NULL
And this is giving me nuts, because normally it is working fine and, for example, with a structure like this:
select id from table_name where email = "xxxxx.xxxxxxxxx#gmail.com"
Returns the id
select id from table_name where email like "xxxxx.xxxxxxxxx#gmail.com"
Returns the id too
#_#
Maybe there's something wrong with the LIKE matching when there is more than one dot in the mailbox structure? Any other idea?
Thanks in advance for your time fellows.

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.

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'

MySQL Find similar strings

I have an InnoDB database table of 12 character codes which often need to be entered by a user.
Occasionally, a user will enter the code incorrectly (for example typing a lower case L instead of a 1 etc).
I'm trying to write a query that will find similar codes to the one they have entered but using LIKE '%code%' gives me way too many results, many of which contain only one matching character.
Is there a way to perform a more detailed check?
Edit - Case sensitive not required.
Any advice appreciated.
Thanks.
Have a look at soundex. Commonly misspelled strings have the same soundex code, so you can query for:
where soundex(Code) like soundex(UserInput)
use without wildcard % for that
SELECT `code` FROM table where code LIKE 'user_input'
thi wil also check the space
SELECT 'a' = 'a ', return 1 whereas SELCET 'a' LIKE 'a ' return 0
reference