[英]Trouble doing a bitwise xor + bit_count in mysql
I am trying to perform a comparison on hash values (hexadecimal strings of 16 characters).我正在尝试对哈希值(16 个字符的十六进制字符串)进行比较。 I have a MYSQL table that stores those values with a phash VARCHAR(16)
column.我有一个 MYSQL 表,它使用phash VARCHAR(16)
列存储这些值。 This is what I am trying to do:这就是我想要做的:
SELECT phash, bit_count(phash ^ x'dda15873a3de013d') FROM mytable;
But the bit_count + xor is not done correctly.但是 bit_count + xor 没有正确完成。 Even for phash='dda15873a3de013d'
I get 33
as a result whereas I should get 0
(the two hex are the same, so the xor should yield only zeros, hence a bit_count of 0.即使对于phash='dda15873a3de013d'
我得到33
结果而我应该得到0
(两个十六进制相同,所以异或应该只产生零,因此 bit_count 为 0。
What is wrong?怎么了? Thank you谢谢
Edit: example here => http://sqlfiddle.com/#!9/d7f5c2/1/0编辑:这里的例子=> http://sqlfiddle.com/#!9/d7f5c2/1/0
mysql> SELECT phash,BIT_COUNT(CONV(phash, 16, 10) ^ 0xdda15873a3de012d) from mytable limit 1;
+------------------+-----------------------------------------------------+
| phash | BIT_COUNT(CONV(phash, 16, 10) ^ 0xdda15873a3de012d) |
+------------------+-----------------------------------------------------+
| dda15873a3de012d | 33 |
+------------------+-----------------------------------------------------+
CONV
returns a string. CONV
返回一个字符串。 You need to cast the result of CONV
to UNSIGNED
.您需要将CONV
的结果CONV
为UNSIGNED
。
The MySQL ^
operator only works on integers. MySQL ^
运算符仅适用于整数。 It can't be used to XOR strings (or blobs) with each other.它不能用于对字符串(或 blob)进行异或。
If your strings specifically represent hexadecimal integers, you can use ^
after converting them to integers:如果您的字符串专门表示十六进制整数,则可以在将它们转换为整数后使用^
:
SELECT BIT_COUNT(CONV(phash, 16, 10) ^ 0xdda15873a3de013d)
SELECT BIT_COUNT(cast(CONV(phash, 16, 10) as unsigned) ^ 0xdda15873a3de013d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.