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

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 網站給出 results 的例子如下:

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學習者快速成長!