mysql表结构如下
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `pic` varchar(50) NOT NULL,
  `hashcode` varchar(16) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '2012120910403250c3fa209bf48.jpg', 'bf8f83818080c0f1');
INSERT INTO `test` VALUES ('2', '2012120620430750c092db26557.JPG', 'ff9880f0f680ceff');
INSERT INTO `test` VALUES ('3', '2012120619582550c08861eb062.jpg', '7f7f004f7f7f7c7f');
INSERT INTO `test` VALUES ('4', '2012112911072650b6d16e7f21f.jpg', '7f7f004f7f7f007f');
其中pic字段为图片名称,hashcode是图片的感知哈希编码(16进制编码字符串,长度固定16位),用户输入一个hashcode,怎么从数据库中找出满足字符串对应位置的字符不同的个数小于5的记录呢?就像“11001”和“11101”对应位置不同字符不同的个数为1,比如
用户输入"7f7f004f7f7f00af",那么第三条和第四条记录是满足的,怎么实现呢?参数为用户输入的hashcode值,需要写存储过程吗?

解决方案 »

  1.   

    这个要拆分`hashcode`、及传过来的值,逐一判断、处理
      

  2.   

    mysql> select * from test;
    +----+---------------------------------+------------------+
    | id | pic                             | hashcode         |
    +----+---------------------------------+------------------+
    |  1 | 2012120910403250c3fa209bf48.jpg | bf8f83818080c0f1 |
    |  2 | 2012120620430750c092db26557.JPG | ff9880f0f680ceff |
    |  3 | 2012120619582550c08861eb062.jpg | 7f7f004f7f7f7c7f |
    |  4 | 2012112911072650b6d16e7f21f.jpg | 7f7f004f7f7f007f |
    +----+---------------------------------+------------------+
    4 rows in set (0.06 sec)mysql> set @x='7f7f004f7f7f00af';
    Query OK, 0 rows affected (0.00 sec)mysql>
    mysql> select *
        -> from test
        -> where length(replace(CONV(CONV(@x,16,10) ^ CONV(hashcode,16,10),10,16),'0',''))<= 5;
    +----+---------------------------------+------------------+
    | id | pic                             | hashcode         |
    +----+---------------------------------+------------------+
    |  3 | 2012120619582550c08861eb062.jpg | 7f7f004f7f7f7c7f |
    |  4 | 2012112911072650b6d16e7f21f.jpg | 7f7f004f7f7f007f |
    +----+---------------------------------+------------------+
    2 rows in set (0.00 sec)mysql>