mysql implicit conversion issue (Case I)

 Construction of the table statement:

CREATE TABLE `user` (
  `id` varchar(255) NOT NULL,
  `username` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  Data in the table:

 

 

The introduction of the problem:

  Implementation of sql statement

  

SELECT id FROM `user` where id = 204027026112927605

  The results as expected, then it should be equal to only one data 204027026112927605 id, the results of the actual output is:

  

 

 Analysis: The condition of the query and then later took place here where implicit conversion

Read line by line into the user table id column val1, whereas macro 204027026112927603 present in the cache, type double type (2.0402702611292762E + 17), where it is passed to the value after val2 = 2.0402702611292762E + 17 to val2.

 

 

 

 

When the scanning of the first row, the value 204027026112927605 turn into doule 2.0402702611292762e17, equation holds, it is determined that qualified rows, continue scanning down, also matches Similarly 204027026112927603

 

Solution type :( consistent, to avoid implicit conversion occurs)


Because when you define the table id is of type varchar type, so the conditions at the conditions we now need to search also assigned to a string type, you can avoid implicit conversions.

Correct sql :( speak with a single quote id value due to)

 

SELECT id FROM `user` where id = '204027026112927605'

 

  Query results:

  

 

 

 

in conclusion:

  1. Avoid occurrence implicit type conversion, an implicit conversion type mainly inconsistent field type, comprising a plurality of parameters in the type, character set, type, or the like collation inconsistency
  2. Implicit type conversion may make it impossible to use the index, the query results are not accurate, and therefore must be carefully screened in the use
  3. Digital types of suggestions in the field definition is defined as int or bigint, when the association table associated field must remain type, character set, collation unanimously

 

 

 Reference knew almost original author:  programmer pathfinder

 

Guess you like

Origin www.cnblogs.com/vegetableDD/p/12546528.html
Recommended