码迷,mamicode.com
首页 > 数据库 > 详细

【坑】 MySQL中,字符串和数值的比较

时间:2017-11-15 00:58:02      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:【坑】 mysql中   字符串和数值的比较   


官方文档:https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html

原文:

Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary.

也就是说在比较的时候,String是可能会被转为数字的。

对于数据开头的字符串,转成数字后会自动丢弃后面的字母部分,只留下纯数字进行比较。

对于没有数字的那些字符串,与数值进行比较的时候,就只剩下0去和其他数值进行比较了。



例子:

1、对于没有数字的那些字符串,与数值进行比较的时候,就只剩下0去和其他数值进行比较了。:

root [(none)] >select 0=‘abc‘;

+---------+

| 0=‘abc‘ |

+---------+

|       1 |

+---------+

1 row in set, 1 warning (0.00 sec)


root [(none)] >show warnings;

+---------+------+-----------------------------------------+

| Level   | Code | Message                                 |

+---------+------+-----------------------------------------+

| Warning | 1292 | Truncated incorrect DOUBLE value: ‘abc‘ |

+---------+------+-----------------------------------------+

1 row in set (0.00 sec)



2、对于数据开头的字符串,转成数字后会自动丢弃后面的字母部分,只留下纯数字进行比较。

root [(none)] >select 11=‘010abc‘;

+-------------+

| 11=‘010abc‘ |

+-------------+

|           0 |

+-------------+

1 row in set, 1 warning (0.00 sec)


root [(none)] >show warnings;

+---------+------+--------------------------------------------+

| Level   | Code | Message                                    |

+---------+------+--------------------------------------------+

| Warning | 1292 | Truncated incorrect DOUBLE value: ‘010abc‘ |

+---------+------+--------------------------------------------+

1 row in set (0.00 sec)



官方网站给的例子更多,更多感兴趣的可以去看看。



其实字符串和数值比较最大的坑在于:它会导致查询不能用到索引,直接就影响了查询的效率。




【坑】 MySQL中,字符串和数值的比较

标签:【坑】 mysql中   字符串和数值的比较   

原文地址:http://lee90.blog.51cto.com/10414478/1981865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!