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

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

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

阿神
阿神

闭关修行中......

répondre à tous(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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!