Jump to content

Recommended Posts

Bonjour!

 

I have come across some strange behaviour in MySQL when performing a simple select.

 

InnoDB Table table1 has one column called Qty. Qty is an datatype INT unsigned column. Therefore the only acceptable values for this column are numeric values and NULL.

 

The query I am performing is

 

SELECT * FROM table1 WHERE Qty = "2Apples"

 

So when I perform the query, I would naturally expect MySQL to return 0 results as the column cannot possibly contain any data that matches "2Apples" due to the alphanumeric searchstring.

 

But it does return results, specifically any value matching "2". It is like MySQL is automatically rewriting the my query to match the following

 

SELECT * FROM table1 WHERE Qty = "2"

 

Does MySQL automatically omit non-numeric characters on a searchstring targeting a column with an INT type? Is it performing some kind of casting on the searchstring due to the column datatype being INT?

 

I notice if I change the query slightly so that the text begins the searchstring, this behaviour disappears and I get 0 results, as intended.

 

SELECT * FROM table1 WHERE Qty = "Apples2"

 

Its not really a problem, just a peculiarity and wanted someone to explain to me why it does this. I have scoured the MySQL documentation but could not find anything related to this.

Yes, it is doing what it is defined to do - http://dev.mysql.com/doc/refman/5.5/en/type-conversion.html

 

See the last rule at that link (in all other cases, the arguments are compared as floating-point (real) numbers.) "2Apples", when parsed as a floating point number results in the value 2 (the first non-numerical character serves as a stop character.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.