12.2 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.

See later in this section for information about the character set
of implicit number-to-string conversions, and for modified rules
that apply to CREATE TABLE ... SELECT
statements.

The following rules describe how conversion occurs for comparison
operations:

If one or both arguments are NULL, the
result of the comparison is NULL, except
for the NULL-safe
<=>
equality comparison operator. For NULL <=>
NULL, the result is true. No conversion is needed.

If both arguments in a comparison operation are strings, they
are compared as strings.

If both arguments are integers, they are compared as integers.

Hexadecimal values are treated as binary strings if not
compared to a number.

If one of the arguments is a
TIMESTAMP or
DATETIME column and the other
argument is a constant, the constant is converted to a
timestamp before the comparison is performed. This is done to
be more ODBC-friendly. Note that this is not done for the
arguments to IN()! To be safe,
always use complete datetime, date, or time strings when doing
comparisons. For example, to achieve best results when using
BETWEEN with date or time values,
use CAST() to explicitly
convert the values to the desired data type.

If one of the arguments is a decimal value, comparison depends
on the other argument. The arguments are compared as decimal
values if the other argument is a decimal or integer value, or
as floating-point values if the other argument is a
floating-point value.

In all other cases, the arguments are compared as
floating-point (real) numbers.

For comparisons of a string column with a number, MySQL cannot use
an index on the column to look up the value quickly. If
str_col is an indexed string column,
the index cannot be used when performing the lookup in the
following statement:

SELECT * FROM tbl_name WHERE str_col=1;

The reason for this is that there are many different strings that
may convert to the value 1, such as
'1', ' 1', or
'1a'.

Comparisons that use floating-point numbers (or values that are
converted to floating-point numbers) are approximate because such
numbers are inexact. This might lead to results that appear
inconsistent:

Such results can occur because the values are converted to
floating-point numbers, which have only 53 bits of precision and
are subject to rounding:

mysql> SELECT '18015376320243459'+0.0;
-> 1.8015376320243e+16

Furthermore, the conversion from string to floating-point and from
integer to floating-point do not necessarily occur the same way.
The integer may be converted to floating-point by the CPU, whereas
the string is converted digit by digit in an operation that
involves floating-point multiplications.

The results shown will vary on different systems, and can be
affected by factors such as computer architecture or the compiler
version or optimization level. One way to avoid such problems is
to use CAST() so that a value is
not converted implicitly to a float-point number:

Implicit conversion of a numeric or temporal value to a string
produces a binary string (a BINARY,
VARBINARY, or
BLOB value). Such implicit
conversions to string typically occur for functions that are
passed numeric or temporal values when string values are more
usual, and thus can have effects beyond the type of the converted
value. Consider the expression CONCAT(1,
'abc'). The numeric argument 1 is
converted to the binary string '1' and the
concatenation of that value with the nonbinary string
'abc' produces the binary string
'1abc'.

For integer expressions, the preceding remarks about expression
evaluation apply somewhat differently for
expression assignment; for example, in a
statement such as this:

CREATE TABLE t SELECT integer_expr;

In this case, the table in the column resulting from the
expression has type INT or
BIGINT depending on the length of
the integer expression. If the maximum length of the expression
does not fit in an INT,
BIGINT is used instead. The length
is taken from the max_length value of the
SELECT result set metadata (see
Section 21.8.5, “C API Data Structures”). This means that you can
force a BIGINT rather than
INT by use of a sufficiently long
expression: