Search code examples
mysqldata-comparison

How does mysql do multi-type comparison?


I was working on a project and due to a miscomprehension, we ended up comparing a stored int with a string in a MySql database. I ran a few test and it seems to work but I would like to know how MySql compares different datatypes.

Does it convert one to the other? If it does does it convert both to strings or to ints?


Solution

  • Mysql manual has a complete section dedicated to this, called Type Conversion in Expression Evaluation.

    When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.

    If you compare an int with a string, then both values are converted to floating point number and compared thus.