看到http://www.2cto.com/database/201202/119999.html 这篇博文,自己验证了一下
create table if not exists `test`( `id` int unsigned not NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, `age` int(5) NOT NULL DEFAULT '0', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(`id`), KEY `idx_name`(`name`), KEY `idx_age`(`age`), KEY `idx_create`(`create_time`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; |
表中插入一些数据
对字符类型的查询时:
从此可以看出对于字符的查询,如果传入数字,则不能用索引,用单引号括起来才能用上索引。
原因是对于数字的1, 字符串‘1’, ‘01’,‘0001’都和它相等。从而就没法使用索引了。
而反过来
可以看出对于数字的传入字符串和数字都能用上索引