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 hexdump使用
# hexdump -h hexdump: invalid option -- 'h' Usage: hexdump [options] file... Options: -b one-byte oc ...
- vue---使用Class
在用vue-cli开发项目的时候,很多时候会用到类.具体的使用方法: config.js(使用类,还可以定义构造函数) class config { /** * 构造函数 * @param {stri ...
- pm2 工具来管理 node 服务端
如下: nodeServer.js 'use strict'; const http = require('http'); const server = http.createServer(funct ...
- Conv2d常设置参数意义-继续扩展
记录下一些常用设置: 1.下采样 当设置为: nn.Conv2d(nc,ndf,,,,bias=False) kernel_size = 4, stride = 2, padding = 1,就是将特 ...
- EF Core基本使用
Mysql: nuget 安装 Pomelo.EntityFrameworkCore.MySql Microsoft.EntityFrameworkCore.Design csprj 修改: < ...
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- PHPExcel 中文使用手册详解 二
$objPHPExcel = new \PHPExcel(); //定义配置 $topNumber = 2;//表头有几行占用 $xlsTitle = iconv('utf-8', 'gb2312', ...
- laravel 配置自动加载多路由文件
在 app\Providers\RouteServiceProvider文件下增加方法&注册: 增加之后就可以在routers下建立api文件夹,在里面添加路由了
- xorm - Update,乐观锁,更新时间updated,NoAutoTime()
更新数据使用Update方法 Update方法的第一个参数为需要更新的内容,可以为一个结构体指针或者一个Map[string]interface{}类型. 当传入的为结构体指针时,只有非nil和非0的 ...
- JavaScript中的原型prototype和__proto__的区别及原型链概念
问题 初学js的同学,总是搞不清楚js中的原型是什么东西,看着控制台打印出来的一串串__proto__,迷惑不已. 例如我定义一个Person,创建一个实例p,并打印实例. function Pers ...