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
MySQL DataType--隐式类型转换的更多相关文章
- MySQL的隐式类型转换整理总结
当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion). 比如下面的例子: 1 2 ...
- MYSQL的隐式类型转换
官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...
- Mysql隐式类型转换原则
MySQL 的隐式类型转换原则: - 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 ...
- mysql的隐式转化
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
- MYSQL隐式类型转换
MYSQL隐式类型转换 关于官方文档中的理解大致是: 如果两个参数比较,有至少一个NULL,结果就是NULL,除了是用NULL<=>NULL 会返回1.不做类型转换 两个参数都是字符串,按 ...
- ORACLE中内部函数SYS_OP_C2C和隐式类型转换
什么是SYS_OP_C2C呢?官方的介绍如下: SYS_OP_C2C is an internal function which does an implicit conversion of varc ...
- ORACLE隐式类型转换
隐式类型转换简介 通常ORACLE数据库存在显式类型转换(Explicit Datatype Conversion)和隐式类型转换(Implicit Datatype Conversion)两 ...
- js条件判断时隐式类型转换
Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...
- dynamic_cast 和 static_cast 隐式类型转换的区别
首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
随机推荐
- Linux下打开超大文件的方法
Linux下打开超大文件方法 在Linux下用VIM打开大小几个G.甚至几十个G的文件时,是非常慢的. 这时,我们可以利用下面的方法分割文件,然后再打开. 1 查看文件的前多少行 head -1000 ...
- Spring中查看加载配置文件中 加载类的个数及详情
断点到: org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinitionCount 显示该 ...
- DEPICT实现基因优化(gene prioritization)、gene set富集分析(geneset enrichment)、组织富集分析(tissue enrichment)
全基因组关联分析除了找到显著的关联位点,我们还可以做基因优化.geneset富集分析.组织富集分析,下面具体讲一讲怎么利用GWAS的summary数据做这个分析. summary数据就是关联分析的结果 ...
- EIGENSTRAT计算PCA的显著性
之前我写过一篇文章群体遗传分析分层校正,该选用多少个PCA?,里面提到可以通过EIGENSTRAT软件确定显著的主成分,后续就可以将显著的主成分加入协变量中. 这篇文章主要是讲如何通过EIGENSTR ...
- 【翻译】Flink Table Api & SQL — 用户定义函数
本文翻译自官网:User-defined Functions https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/tabl ...
- [LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
- mysql 的sql_model模式
原文地址:https://blog.csdn.net/baidu_19338587/article/details/59483954 MySQL的sql_mode合理设置 sql_mode是个很容易被 ...
- Form表单验证组件
Tyrion是一个基于Python实现的支持多个WEB框架的Form表单验证组件,其完美的支持Tornado.Django.Flask.Bottle Web框架.Tyrion主要有两大重要动能: 表单 ...
- ztree实现拖拽移动和复制
1.官网下载ztree:http://www.treejs.cn/v3/api.php 2.引入jquery.ztree.all.min.js 注意,这是基于jQuery的插件,请引入相关js 3.设 ...
- Alpha版本2发布
0.日常开头 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <https://www.cnblogs.com/harry240/p/11524162.html> 团 ...