UNHEX和X之间的区别(MySQL) [英] difference between UNHEX and X (MySQL)

查看:108
本文介绍了UNHEX和X之间的区别(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理数据库中的十六进制值时,MySQL UNHEX和X之间的区别是什么?



例如。

  SELECT * FROM test WHERE guidCol IN(UNHEX('hexadecimalstring')); 

SELECT * FROM test WHERE guidCol IN(X'hexadecimalstring');

两个都给了我确切的结果集。那么有什么区别?性能影响?



编辑: guidCol的基础类型当然是二进制的

解决方案

UNHEX()一个函数,因此您可以执行类似于

覆盖> SET @var ='41';
SELECT UNHEX(@var);
SELECT UNHEX(hex_column)FROM my_table;

X 另一方面是十六进制字符的语法。你不能这样做:
$ b

  SET @var ='41'; 
SELECT X @ var; - 错误(字符串litteral期望)
SELECT X'@ var'; - 错误(`@`不是十六进制数字)
SELECT X(@var); - 返回NULL,不太清楚原因...
SELECT X(hex_column)FROM my_table; - 返回NULL以及

这就解释了为什么你总能在 X :您正在使用语言结构而不是函数调用。 X 不需要评估一个变量,因为它需要一个字形串。


What really is the difference between MySQL UNHEX and X when dealing with hexadecimal values in a database?

Eg.

SELECT * FROM test WHERE guidCol IN (UNHEX('hexadecimalstring'));

SELECT * FROM test WHERE guidCol IN (X'hexadecimalstring');

Both gives me exact result set. So is there any difference? Performance implications?

Edit: the underlying type of guidCol is binary of course

解决方案

UNHEX() is a function, therefore you can do something like

SET @var = '41';
SELECT UNHEX(@var);
SELECT UNHEX(hex_column) FROM my_table;

X, on the other hand, is the syntax for a hexadecimal litteral. You cannot do this:

SET @var = '41';
SELECT X@var; -- error (string litteral expected)
SELECT X'@var'; -- error (`@` is not a hexadecimal digit)
SELECT X(@var); -- returns NULL, not too sure about the reason... [edit: but this is probably why you are inserting NULL values]
SELECT X(hex_column) FROM my_table; -- returns NULL as well

This explains why you always get better performance with X: you are using a language construct instead of a function call. X does not need to evaluate a variable, since it expects a litteral string.

这篇关于UNHEX和X之间的区别(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆