MySQL automatically convert/convert string to number?
P粉336536706
P粉336536706 2023-10-24 08:36:29
0
2
600

Does MySQL automatically convert strings to numeric values?

How does this conversion work?

  • '1234'=1234 ?
  • '1abc' = 1 ?
  • 'Text' = 1 ?

Given thatunits.idis of type bigint, how will this query be interpreted?

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

P粉336536706
P粉336536706

reply all (2)
P粉099145710

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 insql_mode = ''. This way, MySQL will throw an error if an incorrect type is used. Readhttp://dev.mysql.com/doc/ refman/5.0/en/server-sql-mode.htmlfor more details

    P粉330232096

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

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

    Documentation describing type conversions is locatedhere.

    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
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!