some phenomenon of mysql query in where condition compare char filed with int 0

后端 未结 1 560
轻奢々
轻奢々 2021-01-24 23:40

Have a table ,

root@localhost:[test]05:35:05>desc t;
+-----------+----------+------+-----+---------+----------------+
| Field     | Type     | Null | Key | De         


        
相关标签:
1条回答
  • 2021-01-25 00:06

    The cause is how mysql silently converts text to numbers to evaluate the number=text expression as described in mysql's documentation on type conversion in expression evaluation.

    Number=text comparison expressions are evaluated by converting both operands to floating point numbers.

    Text is converted to a number by evaluating the characters from left to right. As long as the characters can be evaluated as a valid number (sign, numbers, decimal point, etc), mysql considers it as a number. The conversion stops in the moment mysql encounters a character that cannot be used in a number (such as a letter), or would result in an ivalid number (such as a 2nd sign).

    Text 'foo1' is converted to 0 because its leftmost character is a letter.

    Text '299a0be4a5a79e6a59fdd251b19d78bb' is converted to 299 because it starts with the characters 299 and then comes the letter a that cannot be interpreted as a number.

    0 讨论(0)
提交回复
热议问题