简体   繁体   English

MySQL将varchar二进制表示字段转换为二进制以使用bit_count进行汉明距离计算

[英]Mysql convert varchar binary representation field to binary to do hamming distance calculation with bit_count

I've a db table with a varchar(64) field to store PHashing data, as 64 chars (1's and 0's ascii characters). 我有一个带varchar(64)字段的数据库表,用于存储PHashing数据,为64个字符 (1和0的ascii字符)。 I need to calculate hamming distance with a test hasta, and it seems that the most efficient way to do is using mysql bit_count function. 我需要用测试hasta计算汉明距离,似乎最有效的方法是使用mysql bit_count函数。 The problem is that I haven't found any way to convert/cast/whatever the hash field in order to be interpreted as a byte(8) instead of varchar(64). 问题是我还没有找到任何方法来转换/广播/散列字段以解释为byte(8)而不是varchar(64)。 Like: 喜欢:

> select hash from data;
"0000000101100111111100011110000011100000111100011011111110011011"
> select convert_to_binary(hash) from data;
0b0000000101100111111100011110000011100000111100011011111110011011

I cannot alter the data and convert all previous data into a binary field. 我无法更改数据并将所有先前的数据转换为二进制字段。 Is there any way to force mysql to re-interpret a field as a literal, or any other alternative solution? 有什么方法可以迫使mysql将字段重新解释为文字,还是任何其他替代解决方案?

I think you should be able to use it like this: 我认为您应该可以像这样使用它:

SELECT BIT_COUNT(CAST(hash AS BINARY)) FROM data; 从数据中选择BIT_COUNT(CAST(hash AS BINARY));

Use CAST to convert the field to BINARY and pass the result to BIT_COUNT . 使用CAST将字段转换为BINARY并将结果传递给BIT_COUNT Casting the field hash to binary will turn it into a string with the binary data type, but BIT_COUNT is able to handle that. 将字段哈希值转换为二进制值会将其转换为二进制数据类型的字符串,但是BIT_COUNT可以处理该字符串。

Working code: 工作代码:

SELECT BIT_COUNT( CONV( hash, 2, 10 ) ^ 
0b0000000101100111111100011110000011100000111100011011111110011011 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM