Home  >  Q&A  >  body text

MySQL automatically convert/convert string to number?

Does MySQL automatically convert strings to numeric values?

How does this conversion work?

Given that units.id is of type bigint, how will this query be interpreted?

SELECT table.* 
FROM table 
WHERE id='text'

P粉336536706P粉336536706361 days ago632

reply all(2)I'll reply

  • P粉099145710

    P粉0991457102023-10-25 00:55:21

    By default, MySQL treats 1 and "1" the same, but you can change this by setting the MySQL behavior to strict mode.

    set @@GLOBAL.sql_mode  = "STRICT_ALL_TABLES";
    set @@SESSION.sql_mode = "STRICT_ALL_TABLES";

    Or you can set these variables in the my.cnf file as permanent variables in sql_mode = ''. This way, MySQL will throw an error if an incorrect type is used. Read http://dev.mysql.com/doc/ refman/5.0/en/server-sql-mode.html for more details

    reply
    0
  • P粉330232096

    P粉3302320962023-10-25 00:05:40

    The answers to the first three questions are: yes, yes, and no.

    When the string 'text' is converted to a number, it becomes the value 0.

    Documentation describing type conversions is located here .

    For your query:

    SELECT table.* 
    FROM table 
    WHERE id='text';

    The rule is captured via a document excerpt:

    In other words, this is actually equivalent to:

    WHERE id = 0.0

    reply
    0
  • Cancelreply