mysql - 使用 where id=201511051505538905 对于 char 类型字段查询的坑?
阿神
阿神 2017-04-17 13:27:55
0
3
624

char 类型存储大数字时,如果查询 sql 直接用 id=201511051505538905 这种不标准的方式查询时,会因为具体 id 值的不同,出现查询不到的情况。 正确做法是加上单引号',便能正确查询到

请问背后的实质原因是什么?

阿神
阿神

闭关修行中......

모든 응답(3)
左手右手慢动作

感谢 @锋云战锦 和 @苏生不惑 的回答, 我从 mysql 官网摘抄了如下解释,能很好的解答我的疑惑:

Comparisons that use floating-point numbers (or values that are converted to floating-point numbers) are approximate because such numbers are inexact. This might lead to results that appear inconsistent:
官网给出的示例如下:

mysql> SELECT '18015376320243458' = 18015376320243458;
        -> 1
mysql> SELECT '18015376320243459' = 18015376320243459;
        -> 0

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

PHPzhong

id=201511051505538905
它的数据类型为int
而你的id的数据类型为string,当然查不到,但你可以转换为int
cast(id as int)

迷茫

where id=201511051505538905 id是char类型 而你用数字比较,mysql会将字符串转为数字,就是0 201511051505538905 != 0

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!