zoukankan      html  css  js  c++  java
  • MySQL DataType--隐式类型转换

    隐式类型转换

    在官方文档中对隐式类型转换规则有如下描述:

    1.	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.
    2.	If both arguments in a comparison operation are strings, they are compared as strings.
    3.	If both arguments are integers, they are compared as integers.Hexadecimal values are treated as binary strings if not compared to a number.
    4.	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.
    5.	A single-row subquery from a table or tables is not considered a constant. For example, if a subquery returns an integer to be compared to a DATETIME value, the comparison is done as two integers. The integer is not converted to a temporal value. To compare the operands as DATETIME values, use CAST() to explicitly convert the subquery value to DATETIME.
    6.	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.
    7.	In all other cases, the arguments are compared as floating-point (real) numbers.

    https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html

    翻译为中文:

    1.	两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换
    2.	两个参数都是字符串,会按照字符串来比较,不做类型转换
    3.	两个参数都是整数,按照整数来比较,不做类型转换
    4.	十六进制的值和非数字做比较时,会被当做二进制串
    5.	有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp
    6.	有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较
    7.	所有其他情况下,两个参数都会被转换为浮点数再进行比较

    由于Float是浮点数,在MySQL中存储的是近似值,当不指定Float的长度和小数位数时,无法使用精确查找进行匹配,执行返回数据为空,查询显示警告信息Empty set。

    解决办法:

    1.	将Float数据类型转换为Double或Decimal数据类型,Decimal数据类型会保留准确精确度数据,而使用Double时不存在该问题。
    2.	为Float指定长度和小数位数
    3.	使用FORMAT函数进行转换,如WHERE FORMAT(C1,3)=FORMAT(123.456,3)
    4.	使用Like进行匹配,如WHERE C1 LIKE 123.456
    
    https://dev.mysql.com/doc/refman/8.0/en/problems-with-float.html

    浮点数近似值问题演示:

    CREATE TABLE tb3001 (
    	id int PRIMARY KEY,
    	c1 float(18, 5)
    );
    
    INSERT INTO tb3001 (id, c1)
    VALUES (1, 100.1);
    
    SELECT @v1 := C1
    FROM TB3001
    WHERE ID = 1;
    
    SELECT @v1;
    
    ## 输出结果:100.0999984741211
  • 相关阅读:
    ecshop后台根据条件查询后不填充table 返回的json数据,content为空?
    smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?
    getJSON回调函数不执行问题?
    高德地图关键字搜索删除上一次搜索的Marker
    多表连接查询详解
    网址图标设置
    CSS 引入方式 选择器
    Html 表单标签 Form
    Html 基本标签
    Python Socket实现简单web服务器
  • 原文地址:https://www.cnblogs.com/gaogao67/p/11001102.html
Copyright © 2011-2022 走看看